Hello, First, order your data by ID and time.
The columns you want in your output dataframe are then unique(ID), tapply( x, ID, function( z ) z[ 1 ] ) and tapply( y, ID, function( z ) z[ lenght( z ) ] - z[ 1 ] ) Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com On Fri, 2009-01-02 at 17:20 +0800, gallon li wrote: > I have the following data > > ID x y time > 1 10 20 0 > 1 10 30 1 > 1 10 40 2 > 2 12 23 0 > 2 12 25 1 > 2 12 28 2 > 2 12 38 3 > 3 5 10 0 > 3 5 15 2 > ..... > > x is time invariant, ID is the subject id number, y is changing over time. > > I want to find out the difference between the first and last observed y > value for each subject and get a table like > > ID x y > 1 10 20 > 2 12 15 > 3 5 5 > ...... > > Is there any easy way to generate the data set? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.