mercredi 19 août 2020

R : Define a variable as 1, each time a condition is met the first time

Trying to set up a new variable for my dataset. It´s supposed to take on the value "1", everytime one of the other variables within the defined group takes on the value 0 for the first time. In the other cases, so e.g. the other variables taking on 0 for the second, third time and so on or being one, the new variable should be 0.

So it should look something like this:

ID   Var1   Var2  VarNew
1     1      1      0
1     1      1      0
1     0      1      1
1     0      1      0
1     0      0      1
2     1      1      0
2     1      1      0
2     1      1      0
2     0      1      1
2     0      1      0

i tried writing a function and using ifelse(), but it did not work. Could anyone help me with this task? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire