Including/Excluding Variables based on the binary variable

I need to include or exclude the variable from a data set based on the factor variable.
If var 1 = 1 then include var2 and var3, otherwise exclude.
I tried:
Newdf<- ifelse((var1 == "1",c(var2),c(-var2)) ,
ifelse((var1 == "1",c(var3),c(-var3))

And with dplyr:
Newdf<- df %>%
select( if_else(var1== 1,var2,-var2))%>%
select( if_else(var1== 1,var3,-var3))
Thank you for your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.