0

我在创建构建列表以传递给 summary_table 的 summaries 参数时遇到问题。当我去使用 summary_table 函数时,它返回

“错误:x必须是公式”

summary_test <- list("Gender" = 
     list("Female" = ~ qwraps2::n_perc0(.mydata$sex == "F"),
           "Male" = ~ qwraps2::n_perc0(.mydata$sex == "M")),
  "Age" =
  list("Mean" = ~ qwraps2::mean_sd(.mydata$age, denote_sd = "paren")),
 "Comorbidities" =
   list("HIV Positive" == ~ qwraps2::n_perc0(.mydata$hiv == 1),
        "Type 2 Diabetes" == ~ qwraps2::n_perc0(.mydata$diabetes == 1)))

whole <- summary_table(mydata, summary_test)
4

3 回答 3

0

最后这奏效了:

summary1 <- list("Age" = 
         list(
           "Mean" = ~ qwraps2::mean_sd(.data$age, digits=1)),
   "Gender" =
       list(
         "Male" = ~ qwraps2::n_perc(.data$sex == "M", digits = 1),
         "Female" = ~ qwraps2::n_perc(.data$sex == "F", digits = 1)),
   "Comorbidities" =
     list(
       "Type 2 Diabetes" = ~ qwraps2::n_perc(.data$diabetes == "1", digits = 1),
       "Past History of PTB" = ~ qwraps2::n_perc(.data$past.ptb == "1", digits = 1),
       "HIV" = ~ qwraps2::n_perc(.data$hiv == "1", digits = 1)
     ))
于 2019-03-20T16:29:46.410 回答
0

摘要将是公式列表的列表,也就是说,最高级别的对象是一个列表,每个元素都是一个列表。这些低级列表的元素都是公式。提供的摘要:

summary_test <- list("Gender" = 
                       list("Female" = ~ qwraps2::n_perc0(.mydata$sex == "F"),
                            "Male"   = ~ qwraps2::n_perc0(.mydata$sex == "M")),
                     "Age" =
                       list("Mean" = ~ qwraps2::mean_sd(.mydata$age, denote_sd = "paren")),
                     "Comorbidities" =
                       list("HIV Positive"    == ~ qwraps2::n_perc0(.mydata$hiv == 1),
                            "Type 2 Diabetes" == ~ qwraps2::n_perc0(.mydata$diabetes == 1)))

==“合并症”的定义,因此有逻辑陈述,而不是公式。

str(summary_test) 
#> List of 3
#>  $ Gender       :List of 2
#>   ..$ Female:Class 'formula'  language ~qwraps2::n_perc0(.mydata$sex == "F")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..$ Male  :Class 'formula'  language ~qwraps2::n_perc0(.mydata$sex == "M")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Age          :List of 1
#>   ..$ Mean:Class 'formula'  language ~qwraps2::mean_sd(.mydata$age, denote_sd = "paren")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Comorbidities:List of 2
#>   ..$ : logi FALSE
#>   ..$ : logi FALSE

此外,.mydata需要用正确的 tidyverse data pronoun 替换.data。正确的语法是:

summary_test <- list("Gender" = 
                       list("Female" = ~ qwraps2::n_perc0(.data$sex == "F"),
                            "Male"   = ~ qwraps2::n_perc0(.data$sex == "M")),
                     "Age" =
                       list("Mean" = ~ qwraps2::mean_sd(.data$age, denote_sd = "paren")),
                     "Comorbidities" =
                       list("HIV Positive"    = ~ qwraps2::n_perc0(.data$hiv == 1),
                            "Type 2 Diabetes" = ~ qwraps2::n_perc0(.data$diabetes == 1)))

str(summary_test)
#> List of 3
#>  $ Gender       :List of 2
#>   ..$ Female:Class 'formula'  language ~qwraps2::n_perc0(.data$sex == "F")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..$ Male  :Class 'formula'  language ~qwraps2::n_perc0(.data$sex == "M")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Age          :List of 1
#>   ..$ Mean:Class 'formula'  language ~qwraps2::mean_sd(.data$age, denote_sd = "paren")
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>  $ Comorbidities:List of 2
#>   ..$ HIV Positive   :Class 'formula'  language ~qwraps2::n_perc0(.data$hiv == 1)
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
#>   ..$ Type 2 Diabetes:Class 'formula'  language ~qwraps2::n_perc0(.data$diabetes == 1)
#>   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>

reprex 包(v0.3.0)于 2019 年 11 月 14 日创建

更新 从 qwraps2 v0.5.0 开始,.data不再需要使用代词。

于 2019-11-14T21:43:13.150 回答
0

没有您的数据就无法复制,但看起来您写的是==不是=在最后一个列表中。尝试:

summary_test <- list("Gender" = 
                   list("Female" = ~ qwraps2::n_perc0(.mydata$sex == "F"),
                        "Male" = ~ qwraps2::n_perc0(.mydata$sex == "M")),
                 "Age" =
                   list("Mean" = ~ qwraps2::mean_sd(.mydata$age, denote_sd = "paren"),
                        "Comorbidities" =
                          list("HIV Positive" = ~ qwraps2::n_perc0(.mydata$hiv == 1),
                               "Type 2 Diabetes" = ~ qwraps2::n_perc0(.mydata$diabetes == 1)))

whole <- summary_table(mydata, summary_test)
于 2019-03-19T15:17:38.893 回答