Re: [R] Collapsing a vector/data-frame based on the previous values

2012-07-25 Thread arun
achandran To: R Project Help Cc: Sent: Tuesday, July 24, 2012 2:40 AM Subject: [R] Collapsing a vector/data-frame based on the previous values Hello I have a data frame like this: dput(states) structure(list(Date = c("24/07/2012", "25/07/2012", "26/07/2012", "

Re: [R] Collapsing a vector/data-frame based on the previous values

2012-07-24 Thread Raghuraman Ramachandran
Many thanks Jim. On Tue, Jul 24, 2012 at 2:25 PM, jim holtman wrote: > Here is another way of doing it: > > > states <- structure(list(Date = c("24/07/2012", "25/07/2012", > "26/07/2012", > + "27/07/2012", "28/07/2012", "24/07/2012", "25/07/2012", "26/07/2012", > + "27/07/2012", "28/07/2012"), S

Re: [R] Collapsing a vector/data-frame based on the previous values

2012-07-24 Thread jim holtman
Here is another way of doing it: > states <- structure(list(Date = c("24/07/2012", "25/07/2012", "26/07/2012", + "27/07/2012", "28/07/2012", "24/07/2012", "25/07/2012", "26/07/2012", + "27/07/2012", "28/07/2012"), State = c(1L, 1L, 1L, 1L, 1L, -1L, + -1L, -1L, 1L, -1L)), .Names = c("Date", "State"

Re: [R] Collapsing a vector/data-frame based on the previous values

2012-07-24 Thread Raghuraman Ramachandran
That helps a lot mate. Thanks. On Tue, Jul 24, 2012 at 9:50 AM, Rui Barradas wrote: > Hello, > > Step by step way: > > > inx <- rle(states$State)$lengths > inx <- cumsum(c(1, inx)) > inx <- inx[inx <= nrow(states)] > states[inx, ] > > Hope this helps, > > Rui Barradas > > Em 24-07-2012 07:40, Ra

[R] Collapsing a vector/data-frame based on the previous values

2012-07-23 Thread Raghuraman Ramachandran
Hello I have a data frame like this: dput(states) structure(list(Date = c("24/07/2012", "25/07/2012", "26/07/2012", "27/07/2012", "28/07/2012", "24/07/2012", "25/07/2012", "26/07/2012", "27/07/2012", "28/07/2012"), State = c(1L, 1L, 1L, 1L, 1L, -1L, -1L, -1L, 1L, -1L)), .Names = c("Date", "State")