Gallon Li
2013-Jun-13 07:56 UTC
[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)> dsid 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]]
Chris Campbell
2013-Jun-13 08:00 UTC
[R] find the position of first observation for each subject
which(!duplicated(ds[, "id"])) Chris Campbell, PhD Tel. +44 (0) 1249 705 450?| Mobile. +44 (0) 7929 628 349 mailto:ccampbell at mango-solutions.com?| http://www.mango-solutions.com Mango Solutions, 2 Methuen Park, Chippenham, Wiltshire , SN14 OGB UK -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Gallon Li Sent: 13 June 2013 08:56 To: R-help at r-project.org 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)> dsid 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. -- LEGAL NOTICE\ \ This message is intended for the use of ...{{dropped:18}}
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.
MacQueen, Don
2013-Jun-14 14:44 UTC
[R] find the position of first observation for each subject
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 National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/13/13 12:56 AM, "Gallon Li" <gallon.li at gmail.com> wrote:>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.