0

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))

Arjen Boon
  • 21
  • 2
  • Could you add some examples of code you've tried? – AodhanOL Jan 08 '18 at 11:17
  • @AodhanOL: I tried `aggregate(location ~ year, df, FUN= function (year) (c(year=2008) & C(year== 2009)))` just to try for those locations that were sampled in 2008 an 2009 but that gave a boolean 'TRUE' for all combinations, which simply is not the case – Arjen Boon Jan 08 '18 at 12:03
  • If you want you question reopened, I suggest you edit it and add in the code example you posted in your comment. If you have any other code examples you tried, adding them as well would be a good idea. – robinCTS Jan 09 '18 at 08:16

0 Answers0