【发布时间】:2015-03-18 21:23:38
【问题描述】:
我想在我的数据框中检查 TRUE、FALSE、TRUE 和其他七种组合,它们有 87027 行和 3 列。我想为每一行写这样的结果;
TRUE TRUE TRUE -> abc
TRUE TRUE FALSE-> ab
TRUE FALSE TRUE->ac
FALSE TRUE TRUE->bc
还有其他...
这是一个简化的示例数据框:
A B C
TRUE FALSE FALSE
TRUE FALSE FALSE
FALSE FALSE FALSE
TRUE FALSE TRUE
FALSE FALSE TRUE
为此我写了一个for循环
for (i in 1:87027) {
if (data$A[i]=="TRUE" & data$B[i]=="TRUE" ) {
if (data$C[i]=="TRUE") {
data$new[i]="abc"
}
}
if (data$A[i]=="TRUE" & data$B[i]=="TRUE" ) {
if (data$C[i]=="FALSE") {
data$new[i]="ab"
}
}
if (data$A[i]=="TRUE" & data$B[i]=="FALSE" ) {
if (data$C[i]=="TRUE") {
data$new[i]="ac"
}
}
if (data$A[i]=="TRUE" & data$B[i]=="FALSE" ) {
if (data$C[i]=="FALSE") {
data$new[i]="a"
}
}
if (data$A[i]=="FALSE" & data$B[i]=="FALSE" ) {
if (data$C[i]=="FALSE") {
data$new[i]="Nothing"
}
}
if (data$A[i]=="FALSE" & data$B[i]=="TRUE" ) {
if (data$C[i]=="TRUE") {
data$new[i]="bc"
}
}
if (data$A[i]=="FALSE" & data$B[i]=="TRUE" ) {
if (data$C[i]=="FALSE") {
data$new[i]="b"
}
}
if (data$A[i]=="FALSE" & data$B[i]=="FALSE" ) {
if (data$C[i]=="TRUE") {
data$new[i]="c"
}
}
}
运行这段代码需要几分钟,我想知道我怎样才能以一种复杂的方式做到这一点
【问题讨论】:
-
那段代码伤了我的心
-
感谢 Dason,我试图以最古老的方式解决我的问题,因为我是 R 的新用户
标签: r multiple-conditions