Search code examples
rdplyrqwraps2

Error when using summary_table in qwraps2. Not correct data.frame?


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) 

Solution

  • 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.