This should set the first two ia==4 entries to 1 in saccade column:
> x <- read.table('/tempxx.txt.r', header=TRUE) > x vp trial ia saccade 1 1 1 5 0 2 1 1 5 0 3 1 1 5 0 4 1 1 5 1 5 1 1 5 1 6 1 1 9 1 7 1 1 9 1 8 1 1 9 1 9 1 1 4 1 10 1 1 4 0 11 1 2 5 0 12 1 2 5 0 13 1 2 5 0 14 1 2 5 1 15 1 2 5 1 16 1 2 9 1 17 1 2 9 1 18 1 2 9 1 19 1 2 4 1 20 1 2 4 1 21 1 2 4 0 22 1 2 4 0 23 1 3 5 0 24 1 3 5 0 25 1 3 5 0 26 1 3 5 1 27 1 3 5 1 28 1 3 9 1 29 1 3 9 1 30 1 3 9 1 31 1 3 4 0 32 1 3 4 0 33 1 3 4 0 34 1 4 5 0 35 1 4 5 0 36 1 4 5 0 37 1 4 5 1 38 1 4 5 1 39 1 4 9 1 40 1 4 9 1 41 1 4 9 1 42 1 4 4 0 43 1 4 4 0 44 1 4 4 0 45 1 4 4 0 46 1 4 4 0 47 1 4 4 0 > # find the runs of 'ia' > z.rle <- rle(x$ia) > # get the offsets of each sequence > z.rle$offset <- cumsum(c(1,z.rle$lengths)) > ia4 <- which(z.rle$values == 4) > for (i in ia4){ + # set all the saccade values to 0 + x$saccade[seq(z.rle$offset[i], length=z.rle$lengths[i])] <- 0 + # set at most the first two to 1 + x$saccade[seq(z.rle$offset[i], length=min(2, z.rle$lengths[i]))] <- 1 + } > > x vp trial ia saccade 1 1 1 5 0 2 1 1 5 0 3 1 1 5 0 4 1 1 5 1 5 1 1 5 1 6 1 1 9 1 7 1 1 9 1 8 1 1 9 1 9 1 1 4 1 10 1 1 4 1 11 1 2 5 0 12 1 2 5 0 13 1 2 5 0 14 1 2 5 1 15 1 2 5 1 16 1 2 9 1 17 1 2 9 1 18 1 2 9 1 19 1 2 4 1 20 1 2 4 1 21 1 2 4 0 22 1 2 4 0 23 1 3 5 0 24 1 3 5 0 25 1 3 5 0 26 1 3 5 1 27 1 3 5 1 28 1 3 9 1 29 1 3 9 1 30 1 3 9 1 31 1 3 4 1 32 1 3 4 1 33 1 3 4 0 34 1 4 5 0 35 1 4 5 0 36 1 4 5 0 37 1 4 5 1 38 1 4 5 1 39 1 4 9 1 40 1 4 9 1 41 1 4 9 1 42 1 4 4 1 43 1 4 4 1 44 1 4 4 0 45 1 4 4 0 46 1 4 4 0 47 1 4 4 0 > On Mon, Aug 3, 2009 at 11:05 AM, Jens Bölte<boe...@psy.uni-muenster.de> wrote: > Hello, > > I have run an eye-tracking experiment for which I now like to analyse the > saccades. Participants looked from a fixation cross (ia = 5) to the target > area (ia = 4) in following example of a data frame. ia = 9 stands for > everything else. A saccade is indicated by saccade = 1. Sometimes the > saccade just ends before the target area (see below). This is due to the > parameters that determine a saccade and a fixation and some measurement > error (accuracy of the eyetracker). I like to include the first two > no-saccades in a target area into the saccade. The simplest approach, at > least to my knowledge, would be to replace the value saccade = 0 with > saccade = 1 for the first two ia = 4 (see the example below). But that is > beyond my rather limited knowledge in R. > My data frame is structured as follows (.... stands for multiple rows; just > deleted for presentation purposes) > > vp trial ia saccade (other columns deleted for presentation purposes) > 1 1 5 0 > 1 1 5 0 > 1 1 5 0 > 1 1 5 1 > 1 1 5 1 > .... > 1 1 9 1 > 1 1 9 1 > 1 1 9 1 > .... > 1 1 4 1 > 1 1 4 0 > ..... > 1 2 5 0 > 1 2 5 0 > 1 2 5 0 > 1 2 5 1 > 1 2 5 1 > .... > 1 2 9 1 > 1 2 9 1 > 1 2 9 1 > .... > 1 2 4 1 > 1 2 4 1 > 1 2 4 0 > 1 2 4 0 > > aso. more trials follow (up 72). > 1 3 5 0 > 1 3 5 0 > 1 3 5 0 > 1 3 5 1 > 1 3 5 1 > .... > 1 3 9 1 > 1 3 9 1 > 1 3 9 1 > .... > 1 3 4 0 <- how do I get -> 1 3 4 1 > 1 3 4 0 <- how do I get -> 1 3 4 1 > 1 3 4 0 <- how do I keep -> 1 3 4 0 > ..... > 1 4 5 0 > 1 4 5 0 > 1 4 5 0 > 1 4 5 1 > 1 4 5 1 > .... > 1 4 9 1 > 1 4 9 1 > 1 4 9 1 > .... > 1 4 4 0 <- how do I get -> 1 4 4 1 > 1 4 4 0 <- how do I get -> 1 4 4 1 1 4 4 0 <- how do I keep -> 1 4 4 0 > 1 4 4 0 <- how do I keep -> 1 4 4 0 > 1 4 4 0 > 1 4 4 0 > 1 4 4 0 > > Thanks a lot for any help > > Jens > > ______________________________________________ > R-help@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? ______________________________________________ R-help@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.