r - Return df with a columns values that occur more than once -
i have data frame df, , trying subset rows have value in column b
occur more once in dataset.
i tried using table it, having trouble subsetting table:
t<-table(df$b)
then try subsetting using:
subset(df, table(df$b)>1)
and error
"error in x[subset & !is.na(subset)] : object of type 'closure' not subsettable"
how can subset data frame using table counts?
here dplyr
solution (using mrflick's data.frame)
library(dplyr) newd <- dd %>% group_by(b) %>% filter(n()>1) # newd # 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)]),]
Comments
Post a Comment