lundi 2 janvier 2017

R finding last instance of string in one column that is in a second column based on condition

I have a data frame in R with four columns. What I want to do is if a condition is satisfied with a row in column A I want to match the value of that row in column C, and find the last instance that the value appeared in column B, and then add a number to that row for column D. Here is an example of what I have, what I want. If df$a==3 then match the value of that row in column C ("Jim") to the last time it appeared in column B, and then add 3 to that row in column D.

Have                                              Want
a    b    c    d                                  a    b    c    d
21   Jim  -    0                                  21   Jim   -   0
2    Jim  -    1                                  2    Jim   -   4
3    Stan Jim  2                                  3    Stan  Jim 2
2    Bill -    4                                  2    Bill  -   4

Aucun commentaire:

Enregistrer un commentaire