繁体   English   中英

r删除不代表所有组的记录

[英]r remove records that dont represent all groups

处理原始数据后,我们获得了以下data.frame

        ItemID    GroupID mentions
1         601          3     1
2         601          4     1
3         611          3     1
4         661          3     1
5         801          3     1
6         821          3     1
6         841          1     3
6         841          2     3
6         841          3     3
6         841          4     3

我有10000条这样的记录,我的首要目标是找出代表所有4个GroupID的项。 首先,我试图通过绘图在视觉上做到这一点。

ggplot(item.stats, aes(x=ItemID, y=mentions, fill=GroupID)) + 
  geom_bar(stat='identity', position='dodge')

对于大型数据集,这看起来并不明智。 最好的方法是了解多少个项目代表所有组并提及提及。

在上面的示例中,过滤后,它只能包含:

        ItemID    GroupID mentions
6         841          1     3
6         841          2     3
6         841          3     3
6         841          4     3

尝试获得有意义的可视化效果:

test.with.id <- transform(test,id=as.numeric(factor(ItemID)))
ggplot(test.with.id, aes(x=id, y=mentions, fill=GroupID)) + 
  geom_histogram(stat='identity', position='stack', binwidth = 2)

可能与此类似。 如何在R中将多个堆叠的直方图绘制在一起?

您可以按ItemID进行分组,然后根据所有4个组ID是否在GroupID列中进行过滤:

df %>% group_by(ItemID) %>% filter(all(1:4 %in% GroupID))

# A tibble: 4 x 3
# Groups:   ItemID [1]
#  ItemID GroupID mentions
#   <int>   <int>    <int>
#1    841       1        3
#2    841       2        3
#3    841       3        3
#4    841       4        3

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM