I would typically use rle() for this kind of thing:
> tmp <- cumsum(rle(id)$lengths)
> c(1, tmp[-length(tmp)]+1)
[1] 1 4 9
It does assume that all rows for each unique value of id are grouped
together, but does not require that the rows be sorted by id.
-Don
--
Don MacQueen
Lawrence Livermor
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
To: Gallon Li
Cc: R help
Sent: Thursday, June 13, 2013 9:40 AM
Subject: Re: [R] find the position of first observation for each subject
HI,
Try this:
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
To: R-help@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
which(!duplicated(ds[, "id"]))
Chris Campbell, PhD
Tel. +44 (0) 1249 705 450 | Mobile. +44 (0) 7929 628 349
mailto:ccampb...@mango-solutions.com | http://www.mango-solutions.com
Mango Solutions, 2 Methuen Park, Chippenham, Wiltshire , SN14 OGB UK
-Original Message-
From: r-help-boun...@r
4 matches
Mail list logo