返回具有多次出现的列值的df [英] Return df with a columns values that occur more than once
本文介绍了返回具有多次出现的列值的df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框df,我试图对所有在列 B
中具有值的行进行子集化。
I have a data frame df, and I am trying to subset all rows that have a value in column B
occur more than once in the dataset.
我尝试使用表格来执行此操作,但是在从表格进行子集设置时遇到了麻烦:
I tried using table to do it, but am having trouble subsetting from the table:
t<-table(df$B)
然后我尝试使用: >
Then I try subsetting it using:
subset(df, table(df$B)>1)
我得到了错误
x [subset&! is.na(subset)]:类型为'closure'的
对象不可子集化
"Error in x[subset & !is.na(subset)] : object of type 'closure' is not subsettable"
如何子集我的子集
推荐答案
这是 dplyr
解决方案(
library(dplyr)
newd <- dd %>% group_by(b) %>% filter(n()>1) #
newd
# a b
# 1 1 1
# 2 2 1
# 3 5 4
# 4 6 4
# 5 7 4
# 6 9 6
# 7 10 6
或者,使用data.table
Or, using data.table
setDT(dd)[,if(.N >1) .SD,by=b]
或使用基数R
dd[dd$b %in% unique(dd$b[duplicated(dd$b)]),]
这篇关于返回具有多次出现的列值的df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文