我有一个名为data.frame
的{{1}},我希望通过查看一列expC
中的字符是否等于查找向量中指定的字符来创建新列:
spc
对于east <-c("AL","CB","CM","CO","EN","HA","HP","IG","IP","LU","NR","PE","RM","SG","SS","WD")
向量中expC$spc
中的字符,如果不返回east
则返回1
。
我非常感谢你的帮助。谢谢。
答案 0 :(得分:2)
试试这个:
expC$ee = as.numeric(expC$spc %in% east)
答案 1 :(得分:0)
使用ifelse
# dummy data
east <- letters[1:5]
expC <- data.frame(spc = sample(letters, 100, replace = T))
expC$newVar <- with(expC, ifelse(spc %in% east, 1, 0))
> head(expC)
spc newVar
1 q 0
2 q 0
3 q 0
4 w 0
5 b 1
6 r 0