mardi 28 mai 2019

How to shift value with inside nested ifelse statement?

my code

re$p_RID <- ifelse((re$group_UID & re$Amount_type=='Draw'),
                           shift(re$id), 'NA')

my data frame look like this after I run the code:

id  user_id  Amount_type group_UID p_RID
30    11        Non        1        NA
31    11        Draw       1        30
54    5         Non        2        NA
322   5         Draw       2        54
21    5         Draw       2        322
13    5         Non        2        NA
2445  5         Draw       2        13
111   44        Non        3        NA
287   44        Draw       3        111

I would like to use the id in first occurrence on amount_type of Non value for each p_RID column.(within the same group_UID, if there are many multiple occurrence of Non value, then treat each of them as the first occurrence) The result should look like this:

id  user_id  Amount_type group_UID p_RID
30    11        Non        1        NA
31    11        Draw       1        30
54    5         Non        2        NA
322   5         Draw       2        54
21    5         Draw       2        54 <- this is where I don't know how to edit
13    5         Non        2        NA
2445  5         Draw       2        13
111   44        Non        3        NA
287   44        Draw       3        111

Aucun commentaire:

Enregistrer un commentaire