-1
我有一个data.frame,并希望从某个单元中获取某个值(如果另一个值在数据帧中)。在数据帧中获取一定的值
我试过应用函数。
n <- c(2, 3, 0 ,1)
s <- c(0, 1, 1, 2)
b <- c("THIS", "FALSE", "NOT", "THIS")
df <- data.frame(n, s, b)
df <- sapply(df$Vals, FUN=function(x){ if(b[x]=="THIS") ? n[x] : s[x] })
我的逻辑是:
if(b at position x is equal to "This") {
add n[x] to the column df$Vals
} else {
add s[x] to the column df$Vals
}
而x
为单行。
任何建议我做错了吗?
我感谢您的回复!
如果有更多条件,我该怎么办?喜欢'if()elseif()else'? – mrquad 2015-02-06 13:55:06
其他条件是什么?这个问题并没有被问到;) – 2015-02-06 13:59:54
是的,我没有问他们。不过,我只想了解我将来如何做到这一点。任何建议我怎么能实现呢? – mrquad 2015-02-06 14:01:19