为 R 中的 summary_table() 的输入创建带有汇总统计信息的列表列表 [英] Creating list of lists with summary statistics for input to summary_table() in R
问题描述
我正在按照此处 创建一个干净的汇总统计表.
I am following the instructions laid out here to create a clean table of summary statistics.
在这些说明中,summary_table() 函数的输入是一个列表列表,如下所示:
In these instructions, the input to the summary_table() function is a list of lists, as shown here:
our_summary1 <-
list("Miles Per Gallon" =
list("min" = ~ min(.data$mpg),
"max" = ~ max(.data$mpg),
"mean (sd)" = ~ qwraps2::mean_sd(.data$mpg)),
"Displacement" =
list("min" = ~ min(.data$disp),
"median" = ~ median(.data$disp),
"max" = ~ max(.data$disp),
"mean (sd)" = ~ qwraps2::mean_sd(.data$disp)),
"Weight (1000 lbs)" =
list("min" = ~ min(.data$wt),
"max" = ~ max(.data$wt),
"mean (sd)" = ~ qwraps2::mean_sd(.data$wt)),
"Forward Gears" =
list("Three" = ~ qwraps2::n_perc0(.data$gear == 3),
"Four" = ~ qwraps2::n_perc0(.data$gear == 4),
"Five" = ~ qwraps2::n_perc0(.data$gear == 5))
)
我的数据集中有 48 个变量,每个变量都有自己的列.有没有一种更简洁的方法让我循环遍历数据框中的所有列以创建像上面那样的对象,而无需像这样手动输入?理想情况下,我更喜欢使用 tidyverse 的解决方案.
I have 48 variables in my dataset, and each variable has its own column. Is there a cleaner way for me to cycle through all the columns in my dataframe to create an object like the one above without typing it out manually like this? I would ideally prefer a solution using the tidyverse.
我考虑做的一件事是将我的数据更改为长格式,然后使用 group_by() 对原始数据中的每一列进行分组,然后使用 summarise().但是,我的理解是这将产生一个列表,而不是像 summary_table() 所必需的列表列表.
One thing I was considering doing was changing my data to long format, then using group_by() to group by each of the columns from the original data, then using summarise(). However, my understanding is that this would yield a single list, not a list of lists like is necessary for summary_table().
如果有与我在这里尝试做的完全不同的创建汇总表的方法,请告诉我.这个看起来是我正在考虑的最简洁的选项.对于每个变量,我希望能够重命名它并包括最小值、最大值、平均值和标准偏差.
If there is a completely different way of creating a summary table than what I am trying to do here, please let me know. This one looked the neatest of the options I was considering. For each variable, I'd like to be able to rename it and include the minimum value, maximum value, mean, and standard deviation.
推荐答案
正如您所指出的,您可以将数据转换为更长的格式并使用 summarize()
.诀窍是在每个汇总中创建一个列表列:
As you noted, you could turn your data to a longer format and use summarize()
. The trick is to create a list column within each summarize:
library(dplyr)
library(tidyr)
summarized <- mtcars %>%
pivot_longer(cols = c(mpg, wt, disp)) %>%
group_by(name) %>%
summarize(lst = list(list(mean = mean(value),
max = max(value),
min = min(value),
sd = sd(value))))
summarized
#> # A tibble: 3 x 2
#> name lst
#> * <chr> <list>
#> 1 disp <named list [4]>
#> 2 mpg <named list [4]>
#> 3 wt <named list [4]>
然后可以使用 tibble 包中的 deframe()
将其转换为列表列表.
This can then be turned into a list of lists with deframe()
from the tibble package.
library(tibble)
result <- deframe(summarized)
str(result)
#> List of 3
#> $ disp:List of 4
#> ..$ mean: num 231
#> ..$ max : num 472
#> ..$ min : num 71.1
#> ..$ sd : num 124
#> $ mpg :List of 4
#> ..$ mean: num 20.1
#> ..$ max : num 33.9
#> ..$ min : num 10.4
#> ..$ sd : num 6.03
#> $ wt :List of 4
#> ..$ mean: num 3.22
#> ..$ max : num 5.42
#> ..$ min : num 1.51
#> ..$ sd : num 0.978
这篇关于为 R 中的 summary_table() 的输入创建带有汇总统计信息的列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!