我有一个包含多个列的数据框,其中一些列包含单个字符,例如:
test <- data.frame("IsA" = c("1.0", "0.5", "0.0"), "IsB" = c("1.0", "0.5", "0.0"), "IsC" = c("0.0", "0.5", "0.0"), "C1" = c("a", "b", "c"), "C2"= c("a", "a", "c"), "C3" = c("a", "b", "b"), "C4" = c("c", "b", "c"))
,并提供:
IsA IsB IsC C1 C2 C3 C4
1 1.0 1.0 0.0 a a a c
2 0.5 0.5 0.5 b a b b
3 0.0 0.0 0.0 c c b c
对于每一行,我想添加另外4列,这样如果C1为“a”且“IsA”大于或等于0.05,则新列值为True。
IsA IsB IsC C1 C2 C3 C4 C1.t C2.t C3.t C4.t
1 1.0 1.0 0.0 a a a c TRUE TRUE TRUE FALSE
2 0.5 0.5 0.5 b a b b TRUE TRUE TRUE TRUE
3 0.0 0.0 0.0 c c b c FALSE FALSE FALSE FALSE
答案 0 :(得分:1)
未针对速度进行优化:
test <- cbind(test,
t(apply(test, 1, function(x) {
a <- as.numeric(x[1:3])
names(a) <- letters[1:3]
b <- x[-(1:3)]
a[b] >= 0.05
})))
IsA IsB IsC C1 C2 C3 C4 1 2 3 4
1 1.0 1.0 0.0 a a a c TRUE TRUE TRUE FALSE
2 0.5 0.5 0.5 b a b b TRUE TRUE TRUE TRUE
3 0.0 0.0 0.0 c c b c FALSE FALSE FALSE FALSE