返回df,其中列值超过一次 [英] Return df with a columns values that occur more then once

查看:103
本文介绍了返回df,其中列值超过一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df,而且我试图将数据集中的所有行都列在 B 中多次出现。



我尝试使用表来做,但是从表中有麻烦:

  t< -table(df $ B)

然后我尝试使用:

 子集(df,表(df $ B)> 1)

我收到错误


x中的错误[subset&! is.na(subset)]:
类型为闭包的对象不是子集


数据框使用表计数?

解决方案

这是一个 dplyr 使用mrFlick的data.frame)

  library(dplyr)
newd< - dd%>%group_by )%>%filter(n()> 1)#
newd
#ab
#1 1 1
#2 2 1
#3 5 4
#4 6 4
#5 7 4
#6 9 6
#7 10 6

或使用data.table

  setDT(dd)[,if(.N> 1)。 SD,by = b] 

或使用基础R

  dd [dd $ b%in%unique(dd $ b [duplicateated(dd $ b)])]] 
pre>

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)

And I get the error

"Error in x[subset & !is.na(subset)] : object of type 'closure' is not subsettable"

How can I subset my data frame using table counts?

解决方案

Here is a dplyr solution (using mrFlick's data.frame)

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 

Or, using data.table

setDT(dd)[,if(.N >1) .SD,by=b]

Or using base R

dd[dd$b %in% unique(dd$b[duplicated(dd$b)]),]

这篇关于返回df,其中列值超过一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆