R:通过多对多映射根据另一列过滤一列
我有一个带有ID列和项目列的数据集。 ID映射到一个或多个项目。该数据集对映射到ID的每个项目都有一行。我想返回包含my_items
的ID。物品的顺序无关紧要。我下面有一个玩具示例。
ID <- c(1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5)
item <- c("a", "b", "c", "a", "b", "c", "d", "a", "b", "d", "b", "a", "c")
df <- data.frame(cbind(ID, item))
df
my_items <- c("a", "b", "c")
我的预期输出仅包括项目ID 1和5。
I have a dataset with an ID column and an item column. An ID is mapped to one or more items. The dataset has a row for each item mapped to an ID. I want to return IDs that contain my_items
. The order of the items does not matter. I have a toy example below.
ID <- c(1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5)
item <- c("a", "b", "c", "a", "b", "c", "d", "a", "b", "d", "b", "a", "c")
df <- data.frame(cbind(ID, item))
df
my_items <- c("a", "b", "c")
My expected output would only include item ID 1 and 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
输出
Output
我们可以在使用
%in%
创建逻辑向量并按“ID”分组后使用all
,还可以使用n_distinct
-output创建条件
We can use
all
after creating a logical vector with%in%
and grouping by 'ID' and also create a condition withn_distinct
-output
如果我们添加
安排
,在这种情况下,我们也可以使用相同的
:If we add
arrange
, we could also useidentical
in this case: