使用下面的简单数据框,我想创建一个新列,其中包含1个实例,其中City =" Toronto"和PostInjury =" 0-1",当City ="蒙特利尔"和PostInjury =" 6-10"和其他一切的3。
我想使用mutate和if_else,但不确定如何使用此组合有条件地将多个列组合重新编码为一个没有中间步骤的新列?我可以使用两个if_else语句来创建两个新列,然后将它们与Tidyr联合组合然后重新编码,但这似乎非常繁琐。
我错过了优雅的东西吗?我有一种感觉。有没有办法以这种方式使用if_else与dplyr,或者使用case_when?
City<-c("Toronto", "Toronto", "Montreal","Ottawa","Montreal",
"Hamilton","Peterborough","Toronto","Hamilton","Montreal")
Client<-c("Cl1","Cl2","Cl3","Cl4","Cl5","Cl6","Cl7","Cl8","Cl9","Cl10")
PostInjury<-c("0-1","6-10","0-1","2-5","6-10","0-1","11-15","0-1","0-1","6-10")
DF<- data.frame(City,Client,PostInjury)
答案 0 :(得分:1)
您可以使用嵌套的ifelse
。使用dplyr
:
DF <- DF %>%
mutate(new_column = if_else(City == "Toronto" & PostInjury == "0-1", 1,
if_else(City == "Montreal" & PostInjury == "6-10", 2, 3)))
使用case_when
:
DF <- DF %>% mutate(new_column =
case_when(
City == "Toronto" & PostInjury == "0-1" ~ 1,
City == "Montreal" & PostInjury == "6-10" ~ 2,
TRUE ~ 3
)
)
或使用base
函数:
DF$new_column <- ifelse(DF$City == "Toronto" & DF$PostInjury == "0-1", 1,
ifelse(DF$City == "Montreal" & DF$PostInjury == "6-10", 2, 3))
或
DF$new_column <- sapply(as.character(interaction(DF$City, DF$PostInjury)),
switch,
"Toronto.0-1" = 1,
"Montreal.6-10" = 2,
3)