[R] Coding question for behavioral data analysis

jabroesch james.broesch at gmail.com
Mon Aug 22 16:08:24 CEST 2011


Thank you both for the replies. While neither produced the exact desired
results, they spurred some new thinking about how to approach the problem. I
came up with a way to get the output desired, but it is probably pretty
clunky. I will post it here anyway, in case someone is interested in the
future. 

TimeG=mydata$Time[mydata$Behavior == "g"]

TimeA=mydata$Time[mydata$Behavior == "a"]
#next line prevents errors for when there are no instances of a given
behavior by creating a blank file
ifelse( (sum(mydata$Time[mydata$Behavior == "a"])==0), TimeA<-0,
TimeA<-TimeA)
outBehavA<-data.frame(matrix(ncol =length(TimeA), nrow =length(TimeG)))

for (j in 1:length(TimeA)){
for (i in 1:length(TimeG)){
outBehavA[i,j]=TimeA[j]-TimeG[i] }}

rowmin=apply(outBehavA, 1, function(x) min(x[x>=0]))
A<-rowmin
t(A)

timedif<-data.frame(A)


TimeG=mydata$Time[mydata$Behavior == "g"]

TimeZ=mydata$Time[mydata$Behavior == "z"]
ifelse( (sum(mydata$Time[mydata$Behavior == "z"])==0), TimeZ<-0,
TimeZ<-TimeZ)
outBehavZ<-data.frame(matrix(ncol =length(TimeZ), nrow =length(TimeG)))

for (j in 1:length(TimeZ)){
for (i in 1:length(TimeG)){
outBehavZ[i,j]=TimeZ[j]-TimeG[i] }}

rowmin=apply(outBehavZ, 1, function(x) min(x[x>=0]))
Z<-rowmin
t(Z)


timedif<-cbind(Z)

#removing all values over 1000miliseconds
timedif<-as.data.frame(timedif)

timedif<-apply(timedif, c(1,2), function(x) ifelse(x > 1000,
x<-NA, x<-x))
timedif<-as.data.frame(timedif)

#then retain only minimum(first behavior)
for (i in 1:nrow(timedif)){
t<-which.min(timedif[i,])
timedif[i,t]<-1
}
timedif<-apply(timedif, c(1,2), function(x) ifelse(x ==1,
x<-x, x<-0))
timedif<-as.data.frame(timedif)

#sumarizing for each subject

number_of_target_behaviors<-nrow(timedif)

#number of times a behavior was responed to within 1000ms

rowsums1<-rowSums (timedif, na.rm = TRUE, dims = 1)
number_of_contingent_responses_across_domains<-sum(rowsums1)


#number_of_contingent_responses_in each domain

sumofcolumns<-colSums (timedif, na.rm = TRUE, dims = 1)

--
View this message in context: http://r.789695.n4.nabble.com/Coding-question-for-behavioral-data-analysis-tp3753151p3760249.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list