You might try using "outer" to create a matrix that will help out:
> Time <- c(1000, 1050, 1100, 1500, 2500, 5000, 6500, 6600, 7000) > Time [1] 1000 1050 1100 1500 2500 5000 6500 6600 7000 > ?outer starting httpd help server ... done > x <- outer(Time, Time, FUN = function(a, b){d <- b-a; (d>=0) & (d <= 1000)}) > x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [2,] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [4,] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [6,] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE [7,] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE [8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE [9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE > This says, reading down the columns, that event 4 occurs after 1, 2 & 3 within the window; event 9 occurs after 7 & 8 within the window; etc. On Thu, Aug 18, 2011 at 1:29 PM, jabroesch <james.broe...@gmail.com> wrote: > Hello all, > I have a question which I have been struggling with for several weeks > now, that I think might be easy for more proficient coders than > myself. I have a large behavioral dataset, with behaviors and the > times (milliseconds) that they occurred. Each subject has a separate > file, and a sample subject file can be generated using the following > syntax: > > Time <- c(1000, 1050, 1100, 1500, 2500, 5000, 6500, 6600, 7000) > Behavior <- c("g", "a", "s", "5", " z", "g", "z", "g", "a") > mydata <- data.frame(Time,Behavior) > > My basic goal is to be able to extract some details about what > behaviors follow another specific behavior within a time window > (say1000 milliseconds). I figured out how to determine if one specific > behavior follows another specific behavior within that window with the > following syntax. > > TimeG=mydata$Time[mydata$Behavior == "g"] > TimeA=mydata$Time[mydata$Behavior == "a"] > out=rep(NA, length(TimeG)) > > for (i in 1:length(TimeG)){tmp = TimeA-TimeG[i] > out[i]=(sum(0 < tmp & tmp <=1000 )>0 ) } > > number_of_behaviors<-length(TimeG) > number_of_affectmirroring<-sum(out) > > This generates 2 values: the number of times that the target behavior > "g" occurred, and the number of times that it was followed by the > behavior "a" within 1000 milliseconds. > > Question: > What I can't seem to figure out is a to generate a count of the number > of times that multiple different types of behaviors immediately follow > a specific behavior within 1000 milliseconds. So say the behavior of > interest is “g” as it is in the example above. I want to determine > 1)what was the next behavior (from a specified list of possible > behaviors bellow) that followed it within 1000 milliseconds. > > Ideally the output would 1 row with be 13 columns. The first column > would be the number of times that the target behavior, "g" in this > example occurs. The next 12 columns would be the number of times that > one of the specific behaviors was the next behavior that followed > within 1000 milliseconds. So one column for each of these behaviors : > a s d z x c v q w e r t. > > The two complicating factors are: 1)there might be multiple behaviors > that followed within 1000 milliseconds, and I only want to count the > first one; and 2)there are additional behaviors that I would like to > ignore (like the "5" in the example above). > > Any help or suggestions are appreciated. > > Thank you, > James Broesch > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Coding-question-for-behavioral-data-analysis-tp3753151p3753151.html > Sent from the R help mailing list archive at Nabble.com. > [[alternative HTML version deleted]] > > > ______________________________________________ > 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 Data Munger Guru 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.