r - 针对 4 个列表检查 data.frame 列(字符)
问题描述
我想根据 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
解决方案
我们可以使用双sapply
循环,对于数据框中的每个元素,我们检查which
列表元素是否存在并获取相应的列表编号。
lst <- list(a, b, c, d)
df$output <- sapply(df$V1, function(x) paste0(which(sapply(lst,
function(y) any(grepl(x,y)))), collapse = ","))
df
# V1 output
#1 book 1,2
#2 worm 2,3
#3 digital 3
#4 context 1,2,3,4
数据
df <- read.table(text = "book
worm
digital
context")
推荐阅读
- gcc - Debian 8.11 上的 cmake 3.0.2 疯了吗?(更新)
- hyperledger-fabric - 启动结构网络 1.4 时出错:无法创建交付客户端:orderer 客户端无法连接到 orderer.example.com:7050
- python - 如何从文本文件中提取特定值?
- ruby-on-rails - 在 Windows 中安装时如何解决 jekyll sass-converter 错误
- java - Java中的按钮单击操作
- ios - 在设备上捆绑 Firebase JavaScript Serverless 代码以在离线时访问
- python - 即使内存地址不同,Pytorch 变量也会更改 numpy 变量
- python - 在 wtform 自定义验证消息中嵌入 raise ValidationError
- java - 从另一个方法运行一个方法时未读取公共变量
- r - 如何将列表转换为数据集?