How to delete groups containing less than 3 rows of data in R? -
this question has answer here:
i'm using dplyr package in r , have grouped data 3 variables (year, site, brood).
i want rid of groups made of less 3 rows. example in following sample remove rows brood '2'. have lot of data while painstakingly hand helpful automate using r.
year site brood parents 1996 1 1 1996 1 1 1996 1 0 1996 1 0 1996 2 1 1996 2 0 1996 3 1 1996 3 1 1996 3 1 1996 3 0 1996 3 1
i hope makes sense , thank in advance help! i'm new r , stackoverflow apologies if way i've worded question isn't good! let me know if need provide other information.
one way use magic n()
function within filter
:
library(dplyr) my_data <- data.frame(year=1996, site="a", brood=c(1,1,2,2,2)) my_data %>% group_by(year, site, brood) %>% filter(n() >= 3)
the n()
function gives number of rows in current group (or number of rows total if there no grouping).
Comments
Post a Comment