mercredi 2 juin 2021

If else Condition in R based on different columns and rows

I have a dataset with an ID column with multiple visits for every ID. I am trying to create a new variable Status, which will check the Visit column and Value column. The conditions are as follows

For visit in 1,2 & 3, if the values are 1,1,1 then 1 For visit in 1,2 & 3, if the values are 0,1,1 then 0 For visit in 1,2 & 3, if the values are 0,0,0 then 0

How do I specify this condition in R ?

Below is a sample dataset

ID Visit Value
1 1 1
1 2 1
1 3 1
2 1 1
2 2 0
2 3 0
3 1 0
3 2 0
3 3 0
4 1 0
4 2 1
4 3 1

Result dataset

ID Visit Value Status
1 1 1 1
1 2 1 1
1 3 1 1
2 1 1 0
2 2 0 0
2 3 0 0
3 1 0 0
3 2 0 0
3 3 0 0
4 1 0 0
4 2 1 0
4 3 1 0

Aucun commentaire:

Enregistrer un commentaire