I have a data frame with the columns ID
and year
(and value
as well, but this is not of relevance for this question:
id year
1 2006
1 2007
1 2008
2 2007
2 2008
2 2009
2 2010
I'd like to add a new column called minyear
, which is the minimal year for each id
, displayed on each row:
id year minyear
1 2006 2006
1 2007 2006
1 2008 2006
2 2007 2007
2 2008 2007
2 2009 2007
2 2010 2007
In SQL, I'd do something like SELECT ID, year, min(year) AS minyear FROM df GROUP BY id
. Is there an R
-y equivalent which does this in an efficient way?