Sunday, January 27, 2013

R scripting problem

Maybe it's just something silly I can't figure out, but I've been banging my head at my computer for the past couple hours. So, I thought I would put this up on the web to elicit help. And yes, I have looked at stackoverflow and other sites for answers, but I've come up short so far.

Here's the issue:

Assume you have a data frame where "Time" values range from 1:10 and you have 3 measures (F1, F2, F3) for each 10 time point, e.g. F1 at time 1, F1 at time 2, etc. The goal is to take this data frame and simply print the mean value for each of the measures at each time point. If so, it should be possible to simply create subsets at each time point and then extract mean values for each measure. So, I wrote a script that does just this. It doesn't work though:

ts.obj <- ts(array(data=NA, dim=c(10, 3)))     #Create a time series for the output.
{for (i in 1:10)                            #Run a loop through each of the time points.
obj.i <- subset(object, Time==i)
mnF1 <- mean(obj.i$F1)           #Get mean values for each measure at time point "i."
mnF2 <- mean(obj.i$F2)
mnF3 <- mean(obj.i$F3)
ts.obj[i,1] <- mnF1                    #Place these mean values into the time series.
ts.obj[i,2] <- mnF2
ts.obj[i,3] <- mnF3 }

Any suggestions?