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 Livermore
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,] 11
[2,] 12
[3,] 13
[4,] 22
[5,] 23
[6,] 24
[7,] 25
[8,] 26
[9,] 31
[10,] 32
[11,] 33
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:
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...@gmail.com
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
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 smartpink...@yahoo.com
To: Gallon Li gallon...@gmail.com
Cc: R help r-help@r-project.org
Sent: Thursday, June 13, 2013 9:40 AM
Subject: Re: [R] find the position of first
5 matches
Mail list logo