r - Select groups which have more than one variable in them -
this question has answer here:
- remove ids occur x times r 2 answers
simple question. let's have dataframe looks this:
data.frame (species=c(a,a,b,c,c,d),dbh=c(5,4,7,1,3,6))
and want exclude species b , d because occur once, how can that?
this can done using either base r
or using other packages. data.table
, convert 'data.frame' data.table
(setdt(df1)
), grouped 'species', if
number of rows greater 1 (.n>1
) , subset of data.table (.sd
)
library(data.table) setdt(df1)[, if(.n>1) .sd, species]
or dplyr
, use filter
after grouping.
library(dplyr) df1 %>% group_by(species) %>% filter(n()>1)
the base r
function ave
can used well. group 'species', length
, convert logical vector , subset
dataset.
df1[with(df1, ave(dbh, species, fun=length)>1),]
or can use table
frequency of elements in 'species'. find names
of elements have more 1 count, use %in%
logical vector , subset before.
tbl <- table(df1$species)>1 df1[df1$species %in% names(tbl)[tbl],]
Comments
Post a Comment