jeudi 6 août 2020

R condition new variable on two variables

So i have encountrered the following problem: I have a dataframe of the following form:

ID   Date        Var1
1    20200101    0
1    20200102    0
1    20200103    0
1    20200104    0
1    20200104    0
2    20200101    0
2    20200102    0
2    20200103    4
2    20200104    7
2    20200105    13

and I would like to define an additional variable, taking on 1, if Var1 > 0 for the first time in the month of January (20200101:20200131) and 0 otherwise, with respect to the IDs. The actual database spreads over 6 months and Var1 is a running total, so if > 0 once, it won´t decrease ever again. So the final frame should look something like this:

ID   Date        Var1   new_var
1    20200101    0       0
1    20200102    0       0
1    20200103    0       0
1    20200104    0       0
1    20200104    0       0
2    20200101    0       1
2    20200102    0       1
2    20200103    4       1
2    20200104    7       1
2    20200105    13      1

Thanks for all your answers!

Aucun commentaire:

Enregistrer un commentaire