Reputation: 181
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 |
Upvotes: 0
Views: 1056
Reputation: 1202
I'd have tried something like this (suppose your initial table is called df
):
status = c()
for(i in 1:4){ #1:4 correspond to the ID you showed us
if(sum(df[df$ID == i,'value'])==3) status=c(status,rep(1,3))
if(sum(df[df$ID == i,'value'])!=3) status=c(status,rep(0,3))
}
df = cbind(df,status)
I hope that it will help you
Upvotes: 1
Reputation: 3386
I believe that case_when
from the dplyr
package is what you need to use. Here more details on that fuction: https://dplyr.tidyverse.org/reference/case_when.html
Upvotes: 0