[R] Aggregate by First case

Karl Ove Hufthammer Karl.Hufthammer at math.uib.no
Fri May 30 09:35:44 CEST 2008


Jojje Andersson:

> I have a dataframe with in one column an id-variable and in another a
> year-variable. One id-number can occur several years. I have sorted the
> dataframe on id then on year so the same id-number is sorted by year with
> the first occurens at top.
> 
> Now I want to make a subset of this dataframe with just the first year the
> id-number occur, so the first case of every id.
> 
> Id         Year
> 123456 2001
> 123456 2002
> 123456 2003
> 655432 2001
> 655432 2002
> 655432 2003

The following should work (even if the data frame was *not* sorted):

  Id = c(123456,123456,123456,655432,655432,655432)
  Year = c(2001,2002,2003,2001,2002,2003)

  d = data.frame(Id, Year)
  aggregate(d, list(Id), min)[,-1]

Output:

        Id Year
  1 123456 2001
  2 655432 2001

-- 
Karl Ove Hufthammer



More information about the R-help mailing list