1

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.

melbez
  • 960
  • 1
  • 13
  • 36

1 Answers1

2

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 evaluate 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

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • If I change the input into map to c(1:48) will this work for columns 1:48 in my dataframe? And are the default names for out the column names? – melbez May 03 '20 at 21:12
  • 1
    @melbez if you are using numeric index, then have to make changes with `.x .x .x` i.e. it would be `names(df)[.x], names(df)[.x], names(df)[.x]` or simply pass the colum names without any changes i.e. `map(names(df)[1:48, ~ ` – akrun May 03 '20 at 21:15
  • Could you please explain what the commands creating the object out mean? I'd like to better understand so I can do things like this myself in the future. – melbez May 03 '20 at 21:21
  • 1
    @melbez Here, I created a string with `sprintf` by interpolating the columnnames, then evaluated the string after parsing with `parse_expr` which is used in the `summary_table` – akrun May 03 '20 at 21:24
  • 1
    I get a slightly different format, but it still works for my purposes. The structure is the same, but the font is different and the background of mine is all white (not white and gray). I wasn't sure if it just looked different because of formatting settings that I don't know about. – melbez May 03 '20 at 21:33
  • 1
    @melbez i think we need `results = 'asis'` – akrun May 03 '20 at 21:34
  • What does that mean? Where would that fit in the code you laid out above? – melbez May 03 '20 at 21:35