With dplyr, I would like to create a new variable new_regsiege with the following conditions:
For each line and "XX" if regsiege=="XX" and nbeta_regXX>0 then new_regsiege=regsiege
if regsiege=="XX" and nbeta_regXX=0 then new_regsiege is one of the regsiege randomly chosen among those which are not null.
Here's my example :
mydf <- data.frame(
regsiege = c("11","24","93"),
nbeta_reg11 = c(0,1,0),
nbeta_reg24 = c(1,1,0),
nbeta_reg93 = c(1,1,1)
)
# Desired output
regsiege nbeta_reg11 nbeta_reg24 nbeta_reg93 new_regsiege
11 0 1 1 93 (could be also "24")
24 1 1 1 24
93 0 0 1 93
I started like this :
mydf %>%
rowwise()
mutate(
new_regsiege = if_else(...
)