[R] Applying a function to categorized data?

Robert Latest boblatest at gmail.com
Thu Apr 12 16:10:04 CEST 2012


Hi all,

I'm just getting started in R. My problem is the following:

I have a data frame (v1) with lots of production data measurements.
Each row contains a single measurement ('ARI_MIT') with a timestamp. I
want to "lump" the data by months with their mean and standard
deviation.

I have already successfully managed to do the lumping by adding
another column to my data frame:

v1$MONTH = strftime(v1$TIMESTAMP, "%y%m")

This makes a nice month-wise boxplot of my data, although I don't have
an idea why:
boxplot(v1$ARI_MIT ~ v1$MONTH)

I don't need this plotted, though, but in the form of a new data frame
with three columns: the month, the mean, and the standard deviation of
all values from that month.

I tried un-stacking v1 into a list of vectors and then looping over
its elements, calculating the mean of each group:

for (i in unstack(v1, v1$ARI_MIT ~ v1$MONTH)) { write(mean(i), "") }

This works, but how do I get the data into a data frame? With the
month labels in a column? They are not avaliable inside the loop body.

I know I need to get a book on R.

Thanks,
robert



More information about the R-help mailing list