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",
"
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
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"
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
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")
5 matches
Mail list logo