我试图比较数据框中两个因子的值,并将结果输入第三个。我正在尝试使用if语句。
风险是我试图添加的新专栏。
X = within(X, {
Risk = if(X$Member == "Member" & X$Predict == "Non Member"){
"Risk"
} else{
"No Risk"
}
})
答案 0 :(得分:0)
使用dplyr
:
library(dplyr)
X %>% mutate(Risk = ifelse(Member == "Member" & Predict == "Non Member",
"Risk", "No Risk"))
基础R:
X$Risk = ifelse(X$Member == "Member" & X$Predict == "Non Member",
"Risk", "No Risk")