简体   繁体   English

data.table-检查一列是否在另一列(列表)中

[英]data.table - check if one column is in another (list) column

I have a data table with a column containing lists. 我有一个数据表,其中的列包含列表。 I want to check if another column is present in the list column as. 我想检查列表列中是否还有另一列。

library(data.table)
dt <- data.table("a" = 1:3, "b" = list(1:2, 3:4, 5:6))

I tried: 我试过了:

dt[, is_a_in_b := a %in% b]
dt
#    a   b is_a_in_b
# 1: 1 1,2     FALSE
# 2: 2 3,4     FALSE
# 3: 3 5,6     FALSE

which does not give the correct result. 这不会给出正确的结果。 The desired table would be 所需的表将是

dt
#    a   b is_a_in_b
# 1: 1 1,2      TRUE
# 2: 2 3,4     FALSE
# 3: 3 5,6     FALSE

You can use the mapply function with applying the function %in% to two vectors: a and b . 您可以通过将%in%函数应用于两个向量来使用mapply函数: ab In effect it takes a pair of vectors (lists) and produces for every index ix the result of a[ix] %in% b[[ix]] . 实际上,它采用一对向量(列表)并为每个索引ix生成a[ix] %in% b[[ix]]

dt[, is_a_in_b := mapply('%in%', a, b)]

> dt
   a   b is_a_in_b
1: 1 1,2      TRUE
2: 2 3,4     FALSE
3: 3 5,6     FALSE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查一个 data.table 列中的所有元素是否在另一个 data.table 列中出现的每个值的最快方法 - Fastest way to check all elements in one data.table column for each value appearance in another data.table column 如何最好地将data.table的一列与同一data.table的另一列连接? - How to best join one column of a data.table with another column of the same data.table? R data.table append 一列到另一列,隐蔽列表到 Z628CB5675FF524F3E719B7AA2F - R data.table append one column to another, covert list to csv 使用data.table根据另一列重新分配一列 - Reassigning one column according to another using data.table 如何使用 data.table 将一列替换为另一列? - How to replace one column with another using data.table? 当只有一行时,data.table将列表添加为列 - data.table add list as column when only one row 如何将一个data.table中的列中的某些行添加到标题下的另一个data.table中? - How to add certain rows from a column in one data.table to another data.table under a heading? 根据另一个不同维度的 data.table 中的多个条件,选择性地更改一个 data.table 中的列 - Selectively alter a column in one data.table based on multiple conditions in another data.table of different dimensions data.table参考列在另一个data.table中的名称 - data.table reference column in another data.table by name 将 data.table 的一列转换为同一 data.table 中的列表 - Turning a column of a data.table into a list in the same data.table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM