-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello,
as far as I understood your problem, this function might do the trick: CountNextBehavior <- function (data.source, interest.behavior, lev.ignore, interest.timeframe) { ## ------------------------------ ## Returns the number of occuring behavior in a given timeframe ## ## Args: ## data.source is the source dataframe, with columns Behavior and ## Time. Behavior is assumed to be a factor and Time an integer ## interest.behavior is the seeked level of the behavior ## lev.ignore is a vector of behavior levels to ignore ## interest.timeframe fixes the time frame for observation count ## ## Returns: ## a matrix named according to the behaviors # First, get rid of unwanted behavioral levels data.source <- with(data.source[!data.source$Behavior %in% lev.ignore, ], data.frame(Time = Time, Behavior = factor(Behavior))) # Creates the return matrix seeked.blevels <- levels(data.source$Behavior) count.behavior <- matrix(rep(0,length(seeked.blevels)), nrow=1, dimnames=list("Count", seeked.blevels)) # Look when the behavior occurs seeked.behavior <- data.source$Behavior == interest.behavior occuring.time <- data.source$Time[seeked.behavior] # Iterate over occuring times for (obs.time in occuring.time) { # Get all the observed behavior in the given timeframe this.timeframe <- data.source$Time > obs.time & data.source$Time <= obs.time + interest.timeframe this.behavior <- data.source$Behavior[this.timeframe] # Get the level of the first observed behavior first.behavior <- this.behavior[1] # Count the number of occurences this.count <- sum(this.behavior == first.behavior) # Add the count to the given behavior count.behavior[first.behavior] <- count.behavior[first.behavior] + this.count } return(count.behavior) } Am 18.08.2011 19:29, schrieb jabroesch: > 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. - -- - -- RD -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJOThLwAAoJEPy6W3H9JxwxdiUH+gItFSU2shX/82vbZdffIs6l NyGgNP6FSQBSI8zJB+jZQG/+g6s18Lf7E1idcvcY9lbaU8jwsL5cj7eeyV3mTKgq HQbDthNkvrrMofQwFbTo5m0DesRMHPzNa9H9SChxXH8hYTxX1eEzEBAtDRsEeBL6 Tx4FbYH6FLBSr7IZ2dlNlw/9QbbLVg1a1w5IkKvyDQUwKPqCtIoCnKX55JYC0CYR S3TeaxC1jJw6mMdJkO2xNsXxKvsU5zS+HC6AeK6GdKzXw76rQucUJExea2Z+tAtc uPBc9gtObP4/BsRnmv5NikCVaZNZzJg8TZZbnh2/Siw1isUSPPtm46P38EUhx94= =ruSU -----END PGP SIGNATURE----- ______________________________________________ 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.