Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table of mean (SD)s

I have a relatively large dataset, and I want to print a table of means and standard deviations for combinations of factors. I would like to have them in a format like this:

         A            B
test1    2.0 (1.0)    5.0 (2.0)
test2    6.3 (3.1)    2.1 (0.7)

Is there an easy way to do this?

The closest I get is using the tables::tabular function (minimal example):

# Example data
df = data.frame(
   group=c('A', 'A',  'A', 'B', 'B', 'B'),
   value=c(1,2,3,6,8,9))

# Print table     
library(tables)
tabular(value ~ group * (mean + sd), df)

... which outputs this:

       group               
       A        B          
       mean  sd mean  sd   
 value 2     1  7.667 1.52

But I haven't figured out a neat way to transform this format to the mean (SD) format above. Note: These examples are very minimal. I will have a larger hierarchy (currently 4 x (mean+sd) columns and 2 x 3 rows) but the fundamental problem is the same.

like image 336
Jonas Lindeløv Avatar asked Jan 21 '26 19:01

Jonas Lindeløv


1 Answers

library(reshape2)

formatted.table <- dcast(df, 'value' ~ group, fun.aggregate = function(x) {
    return(sprintf('%0.1f (%0.1f)', mean(x), sd(x)))
})

# "value"         A         B
#   value 2.0 (1.0) 7.7 (1.5)

Similar to Chris's answer, but a little bit cleaner (and no "test" variable needed).

You can also do this type of aggregation with the dplyr package.

like image 95
jdobres Avatar answered Jan 23 '26 11:01

jdobres