Search code examples
rmatrixdplyroutputsummarize

Output of the dplyr summarize() fundtion


Is there a convenient way to have dplyr::summarize_all() output the results in a more readable format without having to manually rearrange it after the fact?

Ultimately, I'd like to be able to port the output of summarize more easily to tables in Word, etc.

I would like to avoid the work of doing something like what's below.

Thank you

Example:

library(dplyr)
library(e1071) # for skewness() function
# make fake data
a <- rnorm(20)
b <- rnorm(20)
c <- rnorm(20)
x <- rnorm(20)
y <- rnorm(20)
z <- rnorm(20)

# create dataframe
dat = data.frame(a, b, c, x, y, z)

# run summarize()
descriptives <- dat %>% summarize_all(funs(mean, sd, skewness))
descriptives

# rearrange descriptives
matrix(descriptives, 6, 3, 
    dimnames = list(c("a", "b", "c", "x", "y", "z"), 
    c("mean", "SD", "skew")), byrow = FALSE)

# RETURNS
#  mean       SD        skew       
#a 0.1533271  0.8106499 -0.02879986
#b -0.5117311 0.5608904 -0.2668225 
#c 0.1267941  0.8214882 -0.4260682 
#x 0.05337055 0.9817041 -0.1932566 
#y -0.1091145 0.9050062 -0.3409686 
#z -0.3195788 0.8833493 -0.6663437 

Solution

  • library(tidyr)
    library(dplyr)
    library(e1071) # for skewness() function
    # make fake data
    a <- rnorm(20)
    b <- rnorm(20)
    c <- rnorm(20)
    x <- rnorm(20)
    y <- rnorm(20)
    z <- rnorm(20)
    
    # create dataframe
    dat = data.frame(a, b, c, x, y, z)
    
    # run process 
    dat %>% 
      summarize_all(funs(mean, sd, skewness)) %>%
      gather() %>%
      separate(key, c("var","type")) %>%
      spread(type, value)
    
    #   var          mean        sd   skewness
    # 1   a  0.0182792019 0.9098886 -0.3851676
    # 2   b  0.0003444183 0.9815170  0.6032848
    # 3   c -0.2724927337 0.9557808 -0.1961959
    # 4   x -0.2679435647 0.6557561 -1.0111428
    # 5   y -0.1951287997 0.8190830  0.5120989
    # 6   z -0.0395147539 1.2758244  0.0464844