Thank you for your reading. I often find that I need to apply a function to slices of my data, and then bind the outputs. I usually build a loop for that purpose, but I am sure I am doing it wrong, and that in R I should be using a different way of thinking. Can you please help me learn a better way to do this?
With thanks,
adam
rm(m); m=0; # this variable will hold the output of the loop
for (nobs in as.numeric(levels(factor(s1$obs)))) { # go over observer index
for (nses in as.numeric(levels(factor(subset(s1, obs==nobs)$session)))) { # go over session index
ns1=subset(s1, obs== nobs & session==nses & ky %in% c(1,2)); # the data slice of interest
ds=round( clfdMc (ns1),2); cs=round( cfdMc (ns1),2); # apply function to data slice
rw=cbind(nobs,nses,ds[2,3],ds[3,3],ds[2,3]-ds[3,3], cs[1,3],cs[2,3],nobs+nses/10, ds[2,4],ds[3,4],cs[1,4],cs[2,4]) # create a row from function output
m=rbind(m,rw); #print(paste('obs:',nobs,' nses:',nses,'clear d:',ds[2,3],'red d',ds[3,3]))# bind new row to previous rows
}
}
m=data.frame(m[2:dim(m)[1],]) # create a data frame from these results
names(m)=c('obs','ses','D_clear','D_red','diffD','D_cg-1','D_cg+1','mark','C_clear','C_red','C_cg-1','C_cg+1') # give names to column variables