创建带有摘要统计信息的列表列表,以输入到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屋!