I only want to select the ID's that are in my dataframe for all years, from 2013 untill 2016 (so four times). In that case ID's with only four rows are left (panel data, each ID has 1 row for each year). I already made sure my dataframe only covers the years I need (2013, 2014, 2015, and 2016), but I want to exclude the ID's that have less than 4 years/rows in my dataframe.
This is the structure of my dataframe:
tibble [909,587 x 26] (S3: tbl_df/tbl/data.frame)
$ ID : num [1:909587] 12 12 12 12 16 16 16 16...
$ Gender : num [1:909587] 2 2 2 2 1 1 1 1 1 1 ...
..- attr(*, "format.spss")= chr "F10.0"
$ Year : chr [1:909587] "2016" "2013" "2014" "2015" ...
..- attr(*, "format.spss")= chr "F9.3"
$ Size : num [1:909587] 1983 1999 1951 1976 902 ...
$ Costs : num [1:909587] 2957.47 0 0.34 1041.67 0 ...
$ Urbanisation : num [1:909587] 2 3 3 2 3 3 2 2 2 3 ...
$ Age : num [1:909587] 92 89 90 91 82 83 22 23 24 65 ...
How can I achieve that?
Thank you!
dput
. That said, this might work:df %>% group_by(ID) %>% filter(n_distinct(Year) >= 4)
– JasonAizkalns