0
votes

I am trying to make a CSV export of a group of summary stats generated with (similar to):

group_sum <- tapply(df$value, df$group, summary)

I tried using this to export the batch of summary stats:

write.csv(t(as.matrix(group_sum)), file="name.csv")

Although the output is written to a CSV. It is not properly organized. Row1 lists the group names and Row2 includes ALL of the summary stats for each group.

What I need is a header row that lists the types of stats generated (i.e., Min, 1st Qu., Median, Mean, 3rd Qu., Max) and each set of summary stats listed on a separate row below the header row (labeled with the group names in a separate column). Any thoughts on how I can generate the CSV output this way?

1

1 Answers

0
votes

The tapply() function returns a list which write.csv() can't handle. Change the list to a matrix. You didn't give us any data, so I'll use the iris data set in R:

data(iris)
group_sum  <- do.call(rbind, tapply(iris$Sepal.Length, iris$Species, summary))
write.csv(group_sum, file="name.csv")

These functions are documented on their manual pages: help(tapply) (or just ?tapply) and help(write.csv).