[R] Splitting a data frame with break points where factor changes value

jim holtman jholtman at gmail.com
Sat Jan 31 14:17:04 CET 2009


Here is one approach.  You might want to change NAs to 0 if you want
them included in the split

set.seed(1)
x <- sample(c(1, -1), 30, TRUE)
x
# determine where changes occur
change <- cumsum(c(0, diff(x) != 0))
change
split(x, change)


On Sat, Jan 31, 2009 at 5:25 AM, Titus von der Malsburg
<malsburg at gmail.com> wrote:
> I have a data frame called s3.  This data frame has a column called
> saccade which has two levels 1 and -1.
>
>  > head(s3$saccade, 100)
>  [1] NA NA NA NA -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
>  [26] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  1  1  1
>  [51]  1  1  1  1  1  1  1  1  1  1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
>  [76] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
>
> How can I split this data frame into blocks such that a new block
> begins when the value in s3$saccade changes?  Split doesn't seem to work
> here.  It's important the solution is efficient because the data frame
> is huge.
>
> Thanks!
>
>  Titus
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list