I have a dataset that looks like this. For respondents who only picked one favourite fruit, I need to have variable returned that is a factor which is that respondent’s favourite fruit. If a respondent picked more than one favourite fruit, their value should be NA
.
fav_apple<-sample(c(NA,1), size=100, replace=T, prob=c(0.9,0.1))
fav_orange<-sample(c(NA,1), size=100, replace=T, prob=c(0.9,0.1))
fav_banana<-sample(c(NA,1), size=100, replace=T, prob=c(0.9,0.1))
df<-data.frame(fav_apple, fav_orange, fav_banana)
df$id<-seq(1,nrow(df))
I tried tackling this with pivot_longer
and then repivotting, but I end up with duplicate rows.