0

I'm quite new using R, so hopefully this isn't too basic..
I'm trying to create a summary table, using qwraps2. Having followed the well-written tutorial without problems (qwraps2) I run into an error when applying my own dataset.

library(qwraps2)    
summary_tbl1 <-  
  list("Gender" =  
     list("Female" = ~ qwraps2::n_perc0(.data$gender == 0),  
          "Male" = ~ qwraps2::n_perc0(.data$gender == 1)),  
  "Mean age (sd)" = ~ qwraps2::mean_sd(.data$inage),  
  "Age categories" =  
     list("65-74" = ~ qwraps2::n_perc0(.data$age_cat == 1),  
          "75-84" = ~ qwraps2::n_perc0(.data$age_cat == 2),  
          "> 85" = ~ qwraps2::n_perc0(.data$age_cat == 3))  
   )

#making the overall column
c_overall <- summary_table(my_dataset, summary_tbl1)

Error: x must be a formula Call rlang::last_error() to see a backtrace

The backtrace reads as follows:

12. stop(cnd)  
11. rlang::abort(x)  
10. rlang::f_rhs(y)  
9. FUN(X[[i]], ...)  
8. lapply(s, function(y) { rlang::f_rhs(y) })  
7. FUN(X[[i]], ...)  
6. lapply(summaries, function(s) { lapply(s, function(y) { rlang::f_rhs(y) })...  
5. eval(lhs, parent, parent)  
4. eval(lhs, parent, parent)  
3. lapply(summaries, function(s) { lapply(s, function(y) { rlang::f_rhs(y) })...  
2. summary_table.data.frame(new_dataset, summary_tbl1)  
1. summary_table(new_dataset, summary_tbl1) 

I have converted the dataset to a data.frame using as.data.frame, as that is what summary_tablerequires, from what i can understand.

My dataset is imported from STATA (Haven package), could that be the answer, and if that is case - any ideas on how to overcome?

Or could it be related to the size of my dataset (80.300 obs)?

Thanks in advance

Added the summary readout:

summary_tbl1  
$`Gender`  
$`Gender`$`Female`  
~qwraps2::n_perc0(.data$gender == 0)  
$`Gender`$Male  
~qwraps2::n_perc0(.data$gender == 1)  
$`Mean age (sd)`  
~qwraps2::mean_sd(.data$inage)  
$`Age categories`  
$`Age categories`$`65-74`  
~qwraps2::n_perc0(.data$age_cat == 1)  
$`Age categories`$`75-84`  
~qwraps2::n_perc0(.data$age_cat == 2)  
$`Age categories`$`> 85`  
~qwraps2::n_perc0(.data$age_cat == 3) 
Peter
  • 7,460
  • 2
  • 47
  • 68
  • it looks like you've collected everything into a single list called 'Gender'. it appears your data format is gender[(male,female), mean_age, age_categories()]. so you have a list called genders containing an unnamed list, a numeric names mean_age, and a named list called age_categories. – Phi Jan 21 '19 at 08:59
  • What I'm trying to do is to make a "list of lists", according to the example in the link. So "gender", "Mean age" and "age categories" are subheadings within the table. The code works with this structure in the tutorial (albeit, "mean age" will come out as a subheading) But the summary is created as i should. I added the readout to the original post. – T. Hjelholt Jan 21 '19 at 09:12
  • sorry @Phi - you were absolutely right :) when I change the list to remove the rowgroup that has no underlying rownames, then it comes through. Thanks for your help :) – T. Hjelholt Jan 21 '19 at 10:14
  • @Phi if you post your comment as an answer, I can mark it as answered – T. Hjelholt Jan 22 '19 at 08:06

2 Answers2

0

it looks like you've collected everything into a single list called 'Gender'. it appears your data format is gender[(male,female), mean_age, age_categories()]. so you have a list called genders containing an unnamed list, a numeric names mean_age, and a named list called age_categories.

Phi
  • 414
  • 3
  • 7
0

@phi’s answer is correct. To explian in more detail: the summary is expected to be a list of lists. That is, a list hwere each element is a list.

Let’s look at the structure of the provided summary: (EDIT: omitting the .data pronoun as it is no longer recommened as of qwraps2 version 0.5.0, released 1 Sept 2020).

summary_tbl1 <-
  list("Gender" =
       list("Female" = ~ qwraps2::n_perc0(gender == 0),
            "Male"   = ~ qwraps2::n_perc0(gender == 1)
           ),
       "Mean age (sd)" = ~ qwraps2::mean_sd(inage),
       "Age categories" =
         list("65-74" = ~ qwraps2::n_perc0(age_cat == 1),
              "75-84" = ~ qwraps2::n_perc0(age_cat == 2),
              "> 85"  = ~ qwraps2::n_perc0(age_cat == 3)
             )
       )

str(summary_tbl1, max.level = 1)
#> List of 3
#>  $ Gender        :List of 2
#>  $ Mean age (sd) :Class 'formula'  language ~qwraps2::mean_sd(inage)
#>   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Age categories:List of 3

The first and thrid elements are lists, but the second element is a formula. The correct specification for the summary is:

summary_tbl1 <-
  list("Gender" =
         list("Female" = ~ qwraps2::n_perc0(gender == 0),
              "Male"   = ~ qwraps2::n_perc0(gender == 1)),
       "inage" =
         list("Mean age (sd)" = ~ qwraps2::mean_sd(inage)),
       "Age categories" =
         list("65-74" = ~ qwraps2::n_perc0(age_cat == 1),
              "75-84" = ~ qwraps2::n_perc0(age_cat == 2),
              "> 85"  = ~ qwraps2::n_perc0(age_cat == 3))
       )

str(summary_tbl1, max.level = 1)
#> List of 3
#>  $ Gender        :List of 2
#>  $ inage         :List of 1
#>  $ Age categories:List of 3

Created on 2020-09-01 by the reprex package (v0.3.0)

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Catalina 10.15.6      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Denver              
#>  date     2020-09-01                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.9   2020-08-24 [1] CRAN (R 4.0.2)
#>  callr         3.4.3   2020-03-28 [1] CRAN (R 4.0.0)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.0)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.0)
#>  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.0)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.3   2020-07-05 [1] CRAN (R 4.0.0)
#>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.4.6   2020-02-17 [1] CRAN (R 4.0.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.16    2020-07-24 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
Peter
  • 7,460
  • 2
  • 47
  • 68