I am trying to create a table of summary statistics using the methods outlined here, though I am open to alternatives.
In this outline, they recommend creating the list of summary statistics to be input into the summary_table() command like this:
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))
)
Since I have 48 columns in my dataframe (one for each variable), I posted asking if there is another way for me to create the type of object laid out above. Based on the response, I have created my summary object like this:
summarized <- df %>%
pivot_longer(cols = c(1:48)) %>%
group_by(name) %>%
summarize(lst = list(list(
mean = mean(value),
max = max(value),
min = min(value),
sd = sd(value))))
result <- deframe(summarized)
When I put this into the summary_table() function, the function will not run.
table <- summary_table(df, summaries = result)
I get the following error message.
Error: `x` must be a formula
Do you know why the summary_table() function does not seem to be working with the object I'm using? I thought the object I created was comparable to the one laid out as "our_summary1". I'd appreciate any ideas for how to get this to work.
I have looked at this question and this question, which are both about the same error message, but they don't answer my question since they create the object to input into summary_table() in a different way than I do.
Here is one option to create an expression where we loop over the interested column names with map
, create a string expression, with sprintf
by interpolating the names, then eval
uate the expression after parsing with parse_expr
. This would be used in the summary_table
---
title: "new"
author: "akrun"
date: "5/3/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r sumtable, results='asis'}
library(qwraps2)
library(dplyr)
library(purrr)
options(qwraps2_markup = "markdown")
out <- map(c('mpg', 'disp', 'wt'), ~
eval(rlang::parse_expr(sprintf('list("min" = ~ min(.data$%s),
"max" = ~ max(.data$%s),
"mean (sd)" = ~ qwraps2::mean_sd(.data$%s))', .x, .x, .x))))
names(out) <- c("Miles Per Gallonv", "Displacement", "Weight (1000 lbs)" )
summary_table(mtcars, out)
```
-output