Peter Miksza
Peter Miksza

Reputation: 409

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 

Upvotes: 2

Views: 831

Answers (2)

AntoniosK
AntoniosK

Reputation: 16121

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

Upvotes: 2

infominer
infominer

Reputation: 2011

you need to rearrange your data in a tidy format, Read up https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html I did a quick melt, using the reshape2 package. You can do this using tidyr and the equivalent function would be gather(). Here's a handy reference -http://tidyr.tidyverse.org/ my brain is still too used to reshape2, slowly training my self to use tidyr

library(reshape2)
library(dplyr)
library(e1071)
descriptives <-melt(dat) %>%
group_by(variable) %>%
summarize_all(funs(mean, sd, skewness))

Upvotes: 1

Related Questions