I am new to R and I'm trying to subsetting a data frame, but I don't know how to do according to my needs. Specifically, I have a panel data frame ranging from 1987 to 2017, but some information I need are observed on 2005, 2007, 2013 and 2017. As I can assume this information is constant over time, it's sufficient that one individual has been observed at least in one of these years. How can I subset the data frame to have all the individuals along all years, condition on have being observed at least in one of the set 2005, 2009, 2013, 2017? Thank you.
The idea is the following:
pid year
101 1984
101 1985
101 1986
101 1987
102 1984
102 1985
102 1986
102 1987
..
102 2005
102 2006
103 1990
103 1991
103 1992
103 1993
...
103 2005
What I would like is to keep the all information and years for the pid who have at least the observation in 2005 or 2009, or 2013 or 2017.