[R] Obtaining the first /or last record of a subject in a longitudinal study
Chuck Cleland
ccleland at optonline.net
Thu Aug 7 12:59:08 CEST 2008
On 8/7/2008 6:21 AM, Luwis Tapiwa Diya wrote:
> Dear R users,
>
> I was wondering if anyone knows how to obtain(subset) the first and/or the
> last record of a subject in a longitudinal setup.
>
> Normally in SAS one uses first.variable1 and last.variable1. So my question
> is that is there an R way of doing this.
>
> Regards,
How about a combination of aggregate() and head() or tail()?
> library(nlme)
> aggregate(Oxboys[,-1], list(Subject = Oxboys$Subject), head, n=1)
Subject age height Occasion
1 10 -1 126.2 1
2 26 -1 132.2 1
3 25 -1 135.5 1
4 9 -1 132.7 1
5 2 -1 136.9 1
6 6 -1 142.4 1
7 7 -1 141.3 1
8 17 -1 134.9 1
9 16 -1 142.8 1
10 15 -1 137.5 1
11 8 -1 141.7 1
12 20 -1 146.5 1
13 1 -1 140.5 1
14 18 -1 145.5 1
15 5 -1 145.8 1
16 23 -1 144.5 1
17 11 -1 142.5 1
18 21 -1 143.9 1
19 3 -1 150.0 1
20 24 -1 147.8 1
21 22 -1 147.4 1
22 12 -1 149.9 1
23 13 -1 148.9 1
24 14 -1 151.6 1
25 19 -1 156.9 1
26 4 -1 155.7 1
> aggregate(Oxboys[,-1], list(Subject = Oxboys$Subject), tail, n=1)
..
--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list