【发布时间】:2018-08-17 04:40:38
【问题描述】:
我想根据 4 个列表(a、b、c、d)检查一个单词(在数据框中的列中):
if df$word is in a then df$code <- 1
if df$word is in b then df$code <- 2
if df$word is in c then df$code <- 3
if df$word is in d then df$code <- 4
if df$word is in a & b then df$code <- 1 2
if df$word is in a & c then df$code <- 1 3
if df$word is in a & d then df$code <- 1 4
if df$word is in b & c then df$code <- 2 3
if df$word is in b & d then df$code <- 2 4
if df$word is in c & d then df$code <- 3 4
等等
最有效的方法是什么?
示例
df <- data.frame(word = c("book", "worm", "digital", "context"))
a <- c("book", "context")
b <- c("book", "worm", "context")
c <- c("digital", "worm", "context")
d <- c("context")
预期输出:
book 1 2
worm 2 3
digital 3
context 1 2 3 4
【问题讨论】:
-
你可以用 grep 和 if else 来做