Convert a "by" object to a data frame in R
The by
function returns a list, so you can do something like this:
data.frame(do.call("rbind", by(x, column, mean)))
Old thread, but for anyone who searches for this topic:
analysis = by(...)
data.frame(t(vapply(analysis,unlist,unlist(analysis[[1]]))))
unlist()
will take an element of a by()
output (in this case, analysis
) and express it as a named vector.
vapply()
does unlist to all the elemnts of analysis
and outputs the result. It requires a dummy argument to know the output type, which is what analysis[[1]]
is there for. You may need to add a check that analysis is not empty if that will be possible.
Each output will be a column, so t()
transposes it to the desired orientation where each analysis entry becomes a row.
Consider using ddply in the plyr package instead of by. It handles the work of adding the column to your dataframe.