[R] Odp: the first and last observation for each subject

Petr PIKAL petr.pikal at precheza.cz
Fri Jan 2 10:55:50 CET 2009


Hi

r-help-bounces at r-project.org napsal dne 02.01.2009 10:20:23:

> 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

 sapply(split(test$y, test$ID), function(x) tail(x, 1)-head(x,1))

I am leaving formating to the resulting table to you. Hint: aggregate

Best regards
Petr

> 
> 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 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.




More information about the R-help mailing list