dplyr summarize() 函数的输出

Output of the dplyr summarize() fundtion

有没有方便的方法让 dplyr::summarize_all() 以更易读的格式输出结果,而无需事后手动重新排列结果?

最终,我希望能够更轻松地将 summarize 的输出移植到 Word 等表格中

我想避免做下面的事情。

谢谢

示例:

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 

您需要以整齐的格式重新排列您的数据,阅读 https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html 我使用 reshape2 包进行了快速熔化。您可以使用 tidyr 执行此操作,等效函数为 gather()。这是一个方便的参考 -http://tidyr.tidyverse.org/ 脑子还是太习惯reshape2了,慢慢训练自己使用tidyr

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