To Create a New Column from Multiple Column Conditions

Asked 2 years ago, Updated 2 years ago, 392 views

I would like to create a new variable, "category" = 1, and others (sex=0, group=1,2) as 0 if Sex is 1 and group is 3.Please let me know.

df<-data.frame(
    ID=1:4, age=c(43,62,54,55), sex=c(0,1,1,0), group=c(1,2,3,3)
    BP = c (120, 130, 132, 110), BMI = c (21, 26, 23, 19))

r tidyverse

2022-10-22 09:13

1 Answers

If you use ifelse.

df<-df%>%mute(category=ifelse(sex==1&group==3,1,0))


2022-10-22 09:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.