我想创建一个名为“ comments”的新列,该列基于以下条件逻辑输出评论:
Defect_A Defect_B Defect_C Defect_D Defect_E case1 12 3 13 17 9 case2 20 13 6 7 3 case3 11 10 4 20 12 case4 13 7 2 18 3 case5 5 2 15 11 13 case6 8 1 15 19 4 case7 11 7 19 10 1 case8 9 14 15 11 16 case9 18 10 14 16 8 case10 19 7 8 10 2
我将如何去做?我在下面提供了示例代码。
[编辑]
# Sample data set
set.seed(99)
case1 = sample(1:20, 5, replace=FALSE)
case2 = sample(1:20, 5, replace=FALSE)
case3 = sample(1:20, 5, replace=FALSE)
case4 = sample(1:20, 5, replace=FALSE)
case5 = sample(1:20, 5, replace=FALSE)
case6 = sample(1:20, 5, replace=FALSE)
case7 = sample(1:20, 5, replace=FALSE)
case8 = sample(1:20, 5, replace=FALSE)
case9 = sample(1:20, 5, replace=FALSE)
case10 = sample(1:20, 5, replace=FALSE)
test<-data.frame(rbind(case1, case2, case3, case4, case5, case6, case7, case8, case9, case10))
colnames(test)<- c("Defect_A", "Defect_B", "Defect_C", "Defect_D", "Defect_E")
test
# Conditions
any <- c(1,2,3,4) # for condition 1
any_2 <- c(5,6,7,8) # for conditions 2 and 3
any_2_plus <- c(9,10,11,12,13) # for condition 3
any_3 <- c(14,15,16,17,18) # for condition 4
答案 0 :(得分:1)
使用此数据框:
# Sample data set
df = data.frame(Defect_A = sample(1:30, 10, replace=TRUE),
Defect_B = sample(1:30, 10, replace=TRUE),
Defect_C = sample(1:30, 10, replace=TRUE),
Defect_D = sample(1:30, 10, replace=TRUE),
Defect_E = sample(1:30, 10, replace=TRUE))
# Conditions
any <- c(1,2,3,4) # for condition 1
any_2 <- c(5,6,7,8) # for conditions 2 and 3
any_2_plus <- c(9,10,11,12,13) # for condition 3
any_3 <- c(14,15,16,17,18) # for condition 4
您可以使用多个ifelse
df$comments = apply(df,1, function(x) {
ifelse(length(x[x %in% any == TRUE]) >= 1, 'conjoined', ifelse (
length(x[x %in% any_2 == TRUE]) >= 2, 'spina bifida', ifelse (
length(x[x %in% any_2 == TRUE]) >= 1 && length(x[x %in% any_2_plus == TRUE]) >= 1, 'heterodaxy', ifelse (
length(x[x %in% any_3 == TRUE]) >= 3, 'vacterl', 'NA'))))
})
根据需要进行调整的条件