I have a data.frame listing locations that have been sampled several years, and calculated the number of species at those locations. Hence, I have per location a species number for each year.
However, not every location has been sampled each year.
It would look something like this:
Location Year Species
1 2007 3
1 2008 10
2 2008 4
2 2009 5
2 2010 6
3 2007 3
3 2008 10
3 2009 5
3 2010 6
I want to select only those stations that have been sampled each year, and get a data.frame showing only these locations, the relevant years and their species numbers.
In the above example that would obviously be only location 3.
I searched various sites thoroughly but could not find the answer. I guess the answer is quite simple using either aggregate or subset, and I tried various solutions, but to no avail.
Edit: the answers referred to as being duplicate do not answer my question: I want to select the various years, but only return the stations that contain all these years. Answers referred to only supply the lines with the various years, but not conditional on the stations.
Edit
The answer to my question appeared to be adding a column to my data.frame
counting the frequency of the unique values in the column Location, using the following code:
transform(df, freq.loc = ave(seq(nrow(df)), location, FUN=length))