[R] find the position of first observation for each subject
arun
smartpink111 at yahoo.com
Thu Jun 13 16:30:35 CEST 2013
Also, if the ids are ordered and numeric:
which(c(1,diff(ds[,1]))>0)
#[1] 1 4 9
A.K.
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Gallon Li <gallon.li at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, June 13, 2013 9:40 AM
Subject: Re: [R] find the position of first observation for each subject
HI,
Try this:
ds1<- data.frame(id,time)
which(with(ds1,ave(time,id,FUN=seq))==1)
#[1] 1 4 9
A.K.
----- Original Message -----
From: Gallon Li <gallon.li at gmail.com>
To: R-help at r-project.org
Cc:
Sent: Thursday, June 13, 2013 3:56 AM
Subject: [R] find the position of first observation for each subject
suppose I have the following data
id=c(rep(1,3),rep(2,5),rep(3,4))
time=c(seq(1,3),seq(2,6),seq(1,4))
ds=cbind(id,time)
> ds
id time
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 2 2
[5,] 2 3
[6,] 2 4
[7,] 2 5
[8,] 2 6
[9,] 3 1
[10,] 3 2
[11,] 3 3
[12,] 3 4
i want to return a vector that indicates the position of the first
observation for each id. for the above data, i wish to get (1,4,9).
is it possible to get this quickly>?
[[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