I'm fairly new to R and I'm trying to use aggregate
to perform some time series shaping on a dataframe, per subject and for each metric in my dataset. This works beautifully, but I find that the result isn't in a format that's very easy to use. I'd like to be able to transform the results back into the same format as the original dataframe.
Using the iris dataset as an example:
# Split into two data frames, one for metrics, the other for grouping
iris_species = subset(iris, select=Species)
iris_metrics = subset(iris, select=-Species)
# Compute diff for each metric with respect to its species
iris_diff = aggregate(iris_metrics, iris_species, diff)
I'm just using diff
to illustrate that I have a function that shapes the time series, so I get a time series of possibly different length as a result and definitely not a single aggregate value (e.g. mean).
I'd like to transform the result, which seems to be a matrix that has list valued cells to the original "flat" dataframe.
I'm mostly curious about how to manage this with results from aggregate
, but I'd be ok with solutions that do everything in plyr
or reshape
.