An embedded and charset-unspecified text was scrubbed... Name: inte tillg?nglig URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080530/6a8d5938/attachment.pl>
try this:
dat <- read.table(textConnection(
"Id Year
123456 2001
123456 2002
123456 2003
655432 2001
655432 2002
655432 2003"), header = TRUE)
closeAllConnections()
dat[!duplicated(dat$Id), ]
# or
dat[tapply(row.names(dat), dat$Id, head, 1), ]
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Jojje Andersson" <dietaamigo at hotmail.com>
To: <r-help at r-project.org>
Sent: Friday, May 30, 2008 9:13 AM
Subject: [R] Aggregate by First case
> Hello!
>
> I have an maybe a to a simple question but I cant get it right.
>
> 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
>
> Is there a simple command I can do?
>
> Thanks!
>
> Jojje
>
>
> _________________________________________________________________
> [[elided Hotmail spam]]
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
If df is your dataframe, try aggregate(df$Year,list(df$Id),min) --- On Fri, 30/5/08, Jojje Andersson <dietaamigo at hotmail.com> wrote:> From: Jojje Andersson <dietaamigo at hotmail.com> > Subject: [R] Aggregate by First case > To: "r-help at r-project.org" <r-help at r-project.org> > Received: Friday, 30 May, 2008, 5:13 PM > Hello! > > I have an maybe a to a simple question but I cant get it > right. > > 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 > > Is there a simple command I can do? > > Thanks! > > Jojje > > > _________________________________________________________________ > [[elided Hotmail spam]] > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code.
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 2003The 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