Hallo! I have a vector of ID's like so, id <- c(1,2,2,3,3,3,4,5,5) I would like to create a [start,stop] pair of vectors that index the first and last observation per ID. For the ID list above, it would look like 1 1 2 3 4 6 7 7 8 9 I haven't worked with indexes/data manipulation much in R, so any pointers would be helpful. Many thanks! ~~~~~~~~~~~~~~~~~~~ -Robin Jeffries Dr.P.H. Candidate in Biostatistics UCLA School of Public Health rjeffries@ucla.edu 530-624-0428 [[alternative HTML version deleted]]
Tena koe Robin See rle - it doesn't do exactly what you want but by looking at the code (simply enter rle without brackets) you should be able to get there. HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Robin Jeffries > Sent: Wednesday, 26 May 2010 4:15 p.m. > To: r-help at r-project.org > Subject: [R] Counting indexes > > Hallo! > > I have a vector of ID's like so, > id <- c(1,2,2,3,3,3,4,5,5) > > I would like to create a [start,stop] pair of vectors that index the > first > and last observation per ID. > > For the ID list above, it would look like > 1 1 > 2 3 > 4 6 > 7 7 > 8 9 > > I haven't worked with indexes/data manipulation much in R, so any > pointers > would be helpful. > > Many thanks! > > ~~~~~~~~~~~~~~~~~~~ > -Robin Jeffries > Dr.P.H. Candidate in Biostatistics > UCLA School of Public Health > rjeffries at ucla.edu > 530-624-0428 > > [[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.
Robin Jeffries wrote:> Hallo! > > I have a vector of ID's like so, > id <- c(1,2,2,3,3,3,4,5,5) > > I would like to create a [start,stop] pair of vectors that index the first > and last observation per ID. > > For the ID list above, it would look like > 1 1 > 2 3 > 4 6 > 7 7 > 8 9 >which(!duplicated(id)) [1] 1 2 4 7 8 cumsum(rle(id)$lengths) [1] 1 3 6 7 9
This assumes that for a particular id they all occur together in a run:> cbind(start = which(!duplicated(id)), end = which(!duplicated(id, fromLast = TRUE)))start end [1,] 1 1 [2,] 2 3 [3,] 4 6 [4,] 7 7 [5,] 8 9 On Wed, May 26, 2010 at 12:14 AM, Robin Jeffries <rjeffries at ucla.edu> wrote:> Hallo! > > I have a vector of ID's like so, > id <- c(1,2,2,3,3,3,4,5,5) > > I would like to create a [start,stop] pair of vectors that index the first > and last observation per ID. > > For the ID list above, it would look like > 1 1 > 2 3 > 4 6 > 7 7 > 8 9 > > I haven't worked with indexes/data manipulation much in R, so any pointers > would be helpful. > > Many thanks! > > ~~~~~~~~~~~~~~~~~~~ > -Robin Jeffries > Dr.P.H. Candidate in Biostatistics > UCLA School of Public Health > rjeffries at ucla.edu > 530-624-0428 > > ? ? ? ?[[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. >