In the following dataframe for each household, individual combination if "X_1","Y_2" and "Z_3" all three variable > 0 then add new column "criteria" = "C1" else 0.
Only household 1001 - individual 1 fulfill this condition
I tried ifelse option first and then select_at but to no avail. Its throwing error
data %>% group_by(household,individual) %>%
mutate(criteria = ifelse(X_1 >0 & Y_2 >0 & X_3 >0,"C1",0))
# option_2
data %>% group_by(household,individual) %>%
select_at(vars(X_1 >0 & Y_2 >0 & Z_3 >0,"C1",0),all_vars(.>0)) %>%
mutate(criteria = "c1")
I also want to retain all other variables intact for household - individual combination like year, week, duration in the final dataframe which are not present in the group by.
Please suggest
sample dataset:
data <- data.frame(household=c(1001,1001,1001,1001,1001,1002,1002,1002,1003,1003,1003),
individual = c(1,1,1,1,1,2,2,2,1,1,1),
year = c(2021,2021,2022,2022,2022,2021,2022,2022,2022,2022,2022),
week =c("w51","w52","w1","w2","w4","w51","w1","w3","w1","w2","w3"),
duration =c(20,23,24,56,78,12,34,67,87,89,90),
X_1 = c(3,3,3,3,3,0,0,0,1,1,1),
Y_2 = c(2,2,2,2,2,1,1,1,0,0,0),
Z_3 = c(4,4,4,4,4,0,0,0,0,0,0))
