Re: [R] obtaining first and last record for rows with same identifier

From: Frank E Harrell Jr <f.harrell_at_vanderbilt.edu>
Date: Wed 25 May 2005 - 22:20:28 EST

Francisco J. Zagmutt wrote:
> If you want to obtain a data frame you can use the functions head and
> tail like:
>
> dat=data.frame(id=rep(1:5,3),num=rnorm(15), num2=rnorm(15))#Creates data
> frame with id
> last=do.call("rbind",by(dat,dat$id,tail,1))#Selects the last observation
> for each id
> first=do.call("rbind",by(dat,dat$id,head,1))#Selects the first
> observation for each id
> newdat=rbind(first,last)#Joins data
> newdat=newdat[order(newdat$id),]#sorts data by id
>
> Notice that rownames will give you the original row location of the
> observations selected
>
> I hope this helps
>
> Francisco
>
. . .

You might also look at section 4.3 of
http://biostat.mc.vanderbilt.edu/twiki/pub/Main/RS/sintro.pdf

-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed May 25 22:23:45 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:05 EST