Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a function to create the "list of lists" #62

Closed
estephanmoana opened this issue Nov 21, 2018 · 3 comments
Closed

Using a function to create the "list of lists" #62

estephanmoana opened this issue Nov 21, 2018 · 3 comments

Comments

@estephanmoana
Copy link

estephanmoana commented Nov 21, 2018

Hi, I've been using qwraps2 to create nice looking tables and it has worked like a charm. Now I'm trying to automate making the "list of lists" using a function, however I have not been able to make it work. Here's a draft of the function:

summ.stats <- function(data, var = "varname", vardescr = "Variable") {
    ### Loading the needed packages
    library(dplyr) # Load dplyr package for data manipulation
    library(qwraps2) # Load qwraps2 package for generating tables with descriptive stats
    ## Define the markup language we are working in. # options(qwraps2_markup = "latex") is also supported.
    options(qwraps2_markup = "markdown")
   
    summary <- with(data,
                     list(vardescr.name = c("mean (sd)" = ~qwraps2::mean_sd(var),
                                            "min" = ~min(var),
                                            "max" = ~max(var),
                                            "median (IQR)" = ~qwraps2::median_iqr(var),
                                            )
                          )
                     )
}

The output of this function should be a list-of-lists to pass to the summaries argument of summary_table:
summ_table <- summ.stats(data, "var")
summary_table(data, summ_table)

Can you provide any pointers on how to make such function work? Thank you.

@dewittpe
Copy link
Owner

@estephanmoana, Thanks for the question. I'm glad to know qwraps2 has been useful for you.

The current version of qwraps2 on github may have the tool you are looking for. Look at the qsummary. The summary-statistics vignette may also provide some insight for you too.

@dewittpe dewittpe added this to the Version 0.4.0 milestone Dec 28, 2018
@dewittpe dewittpe mentioned this issue Dec 31, 2018
@dewittpe
Copy link
Owner

@estephanmoana, here is an example using qsummary which will be part of version 0.4.0 being pushed to CRAN very soon.

library(qwraps2)
options(qwraps2_markup = "markdown")

my_data <- mtcars[c("mpg", "wt")]

summ_stats <-
  qsummary(my_data,
           numeric_summaries = list("mean (sd)" = "~ qwraps2::mean_sd(%s)",
                                    "min"       = "~ qwraps2::frmt(min(%s))",
                                    "max"       = "~ qwraps2::frmt(max(%s))",
                                    "median (IQR)" = "~ qwraps2::median_iqr(%s)"))
names(summ_stats)
# [1] "mpg" "wt" 

names(summ_stats)[2] <- "Weight"
names(summ_stats)
# [1] "mpg"    "Weight"

summary_table(my_data, summ_stats)
my_data (N = 32)
mpg   
   mean (sd) 20.09 ± 6.03
   min 10.40
   max 33.90
   median (IQR) 19.20 (15.43, 22.80)
Weight   
   mean (sd) 3.22 ± 0.98
   min 1.51
   max 5.42
   median (IQR) 3.33 (2.58, 3.61)

@estephanmoana
Copy link
Author

Hi, took me a while but I developed a function that makes it a bit easier (at least for me) to automate making tables using qwraps2. See code attached, I hope this might help others using your nice package. Cheers.

table_create.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants