1

total beginner and very hopeful someone can help me(: Wrote the following code to create a statistics summary table grouped by city, but my table turned out weird looking (instead of looking like a table, it displayed the table spacers). What should I do?

load('myData.RData')

#install.packages('qwraps2')
library(qwraps2)
options(qwraps2_markup = 'markdown')
summary_statistics <-
  list(
    "Hobby(hours/week)" =
      list(
        "mean (sd)" = ~qwraps2::mean_sd(myData$hobby_hr_week, na_rm = TRUE),
        "min" = ~min(myData$hobby_hr_week, na.rm = TRUE),
        "max" = ~max(myData$hobby_hr_week, na.rm = TRUE)
      ),
    "Work(hours/week)" =
      list(
        "mean (sd)" = ~qwraps2::mean_sd(myData$work_hr_week, na_rm = TRUE),
        "min" = ~min(myData$work_hr_week, na.rm = TRUE),
        "max" = ~max(myData$work_hr_week, na.rm = TRUE)
      ),
    "Wellness" =
      list(
        "mean (sd)" = ~qwraps2::mean_sd(myData$wellness, na_rm = TRUE),
        "min" = ~min(myData$wellness, na.rm = TRUE),
        "max" = ~max(myData$wellness, na.rm = TRUE)
      ),
    
    "Happiness" =
      list(
        "Happiness" = ~qwraps2::n_perc(myData$RU_happy)

      )
  )



how the table looks like:

|Summary Statistics Table for the Wellness Data Set |myData$city: Eilat (N = 25) |myData$city: jerusalem (N = 25) |myData$city: Metula (N = 25) |myData$city: TelAviv (N = 25) |
|:--------------------------------------------------|:---------------------------|:-------------------------------|:----------------------------|:-----------------------------|
|**Hobby(hours/week)**                              |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; mean (sd)                             |15.54 &plusmn; 4.49         |15.54 &plusmn; 4.49             |15.54 &plusmn; 4.49          |15.54 &plusmn; 4.49           |
|&nbsp;&nbsp; min                                   |3.926501                    |3.926501                        |3.926501                     |3.926501                      |
|&nbsp;&nbsp; max                                   |27.00809                    |27.00809                        |27.00809                     |27.00809                      |
|**Work(hours/week)**                               |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; mean (sd)                             |30.45 &plusmn; 19.51        |30.45 &plusmn; 19.51            |30.45 &plusmn; 19.51         |30.45 &plusmn; 19.51          |
|&nbsp;&nbsp; min                                   |1.945099                    |1.945099                        |1.945099                     |1.945099                      |
|&nbsp;&nbsp; max                                   |68.70944                    |68.70944                        |68.70944                     |68.70944                      |
|**Wellness**                                       |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; mean (sd)                             |-56.11 &plusmn; 100.01      |-56.11 &plusmn; 100.01          |-56.11 &plusmn; 100.01       |-56.11 &plusmn; 100.01        |
|&nbsp;&nbsp; min                                   |-259.3496                   |-259.3496                       |-259.3496                    |-259.3496                     |
|&nbsp;&nbsp; max                                   |144.8053                    |144.8053                        |144.8053                     |144.8053                      |
|**Happiness**                                      |&nbsp;&nbsp;                |&nbsp;&nbsp;                    |&nbsp;&nbsp;                 |&nbsp;&nbsp;                  |
|&nbsp;&nbsp; Happiness                             |35 (35.00%)                 |35 (35.00%)                     |35 (35.00%)                  |35 (35.00%)                   |
Net_D
  • 33
  • 4
  • I feel like I'm missing something here... with `options(qwraps2_markup = 'markdown')` you're asking it to give you Markdown output, yes? Then it gives you Markdown for a table.... What's the problem? – duckmayr Jul 26 '20 at 12:03
  • Hi, tnx, the problem is that it displays different markers instead of simply applying them to the table graphically (&nbsp, &plusmn, ** ...**) – Net_D Jul 26 '20 at 12:15
  • For that you'll need to actually render the Markdown, no different than if you'd asked for `options(qwraps2_markup = 'latex')`---you'd have had to compile the LaTeX to get the graphical output you want. Markdown isn't magic; it has to be rendered. – duckmayr Jul 26 '20 at 12:23

2 Answers2

1

So you have to actually render the Markdown to get the graphical output you wanted.

I copied the Markdown your R command spit out into a new R Markdown file. After escaping the dollar signs ($ to \$) due to how RStudio's knit function works, when I knit, I get the following table:

enter image description here

duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • Thank you! moved my script to a code chunk in a .Rmd file and knitted it- it worked! – Net_D Jul 27 '20 at 13:19
  • Great, glad it helped! Don't forget to mark the answer as accepted [as described in the Help Center](https://stackoverflow.com/help/someone-answers) – duckmayr Jul 27 '20 at 13:31
  • Hi, sorry, I'm using knitr as well, which runs fine, but then I still get an output similar to op, any ideas? – James Nov 07 '20 at 23:15
  • 1
    @James hmm... might be hard to say without a reproducible example. I'd post a new question that shows your markdown code, as well as listing your OS, r version, and rstudio version, as well as linking this question – duckmayr Nov 08 '20 at 01:29
0

duckmayr’s answer addresses the posted question. However, I would be remiss if I did not draw attention to a major error in the construction of summary_statistics. Every summary statistic is defined by myData$<variable> which means that the grouping function fails to look at the results within the group (city name). Look at the values in the table - the same summary statistics are in each and every column.

I expect part of the problem is that prior to version 0.5.0 of qwraps2 the use of the data pronoun .data was suggests/needed. However, as of the release of version 0.5.0 (published on CRAN on 1 September 2020) the summary_table method have been refactored to avoid the use of the data pronoun .data and to support, but not require, dplyr.

For example, let’s build and example data set:

set.seed(42)
myData <-
  data.frame(city          = gl(n = 4, k = 25, labels = c("Eilat", "Jerusalem", "Metula", "TelAviv")),
             hobby_hr_week = rpois(n = 100, lambda = 15.54),
             work_hr_week  = rpois(n = 100, lambda = 30.34),
             wellness      = rnorm(n = 100, mean = -56.11, sd = 100.01),
             RU_happy      = rbinom(n = 100, size = 1, p = 0.35))

Load and attach qwraps2

library(qwraps2)
options(qwraps2_markup = "markdown")
packageVersion("qwraps2")
#> [1] '0.5.0'

Defining the summary_table as in the original question post:

summary_statistics <-
  list(
    "Hobby(hours/week)" =
      list(
           "mean (sd)" = ~ qwraps2::mean_sd(myData$hobby_hr_week, na_rm = TRUE),
           "min"       = ~ min(myData$hobby_hr_week, na.rm = TRUE),
           "max"       = ~ max(myData$hobby_hr_week, na.rm = TRUE)
           ),
       "Work(hours/week)" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(myData$work_hr_week, na_rm = TRUE),
              "min"       = ~ min(myData$work_hr_week, na.rm = TRUE),
              "max"       = ~ max(myData$work_hr_week, na.rm = TRUE)
              ),
       "Wellness" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(myData$wellness, na_rm = TRUE),
              "min"       = ~ min(myData$wellness, na.rm = TRUE),
              "max"       = ~ max(myData$wellness, na.rm = TRUE)
              ),

       "Happiness" =
         list(
              "Happiness" = ~qwraps2::n_perc(myData$RU_happy)

         )
  )

we’ll get a table, as posted in the original question, but again, the value are incorrect, they are the value for the whole data set because myData prefaces each of the variable names.

summary_table(myData, summary_statistics)
#> 
#> 
#> |                       |myData (N = 100)      |
#> |:----------------------|:---------------------|
#> |**Hobby(hours/week)**  |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |15.76 &plusmn; 4.06   |
#> |&nbsp;&nbsp; min       |4                     |
#> |&nbsp;&nbsp; max       |27                    |
#> |**Work(hours/week)**   |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |29.40 &plusmn; 5.73   |
#> |&nbsp;&nbsp; min       |19                    |
#> |&nbsp;&nbsp; max       |43                    |
#> |**Wellness**           |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |-51.84 &plusmn; 97.33 |
#> |&nbsp;&nbsp; min       |-391.075901351521     |
#> |&nbsp;&nbsp; max       |209.058254168693      |
#> |**Happiness**          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Happiness |34 (34.00%)           |

summary_table(myData, summary_statistics, by = "city")
#> 
#> 
#> |                       |Eilat (N = 25)        |Jerusalem (N = 25)    |Metula (N = 25)       |TelAviv (N = 25)      |
#> |:----------------------|:---------------------|:---------------------|:---------------------|:---------------------|
#> |**Hobby(hours/week)**  |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |15.76 &plusmn; 4.06   |15.76 &plusmn; 4.06   |15.76 &plusmn; 4.06   |15.76 &plusmn; 4.06   |
#> |&nbsp;&nbsp; min       |4                     |4                     |4                     |4                     |
#> |&nbsp;&nbsp; max       |27                    |27                    |27                    |27                    |
#> |**Work(hours/week)**   |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |29.40 &plusmn; 5.73   |29.40 &plusmn; 5.73   |29.40 &plusmn; 5.73   |29.40 &plusmn; 5.73   |
#> |&nbsp;&nbsp; min       |19                    |19                    |19                    |19                    |
#> |&nbsp;&nbsp; max       |43                    |43                    |43                    |43                    |
#> |**Wellness**           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd) |-51.84 &plusmn; 97.33 |-51.84 &plusmn; 97.33 |-51.84 &plusmn; 97.33 |-51.84 &plusmn; 97.33 |
#> |&nbsp;&nbsp; min       |-391.075901351521     |-391.075901351521     |-391.075901351521     |-391.075901351521     |
#> |&nbsp;&nbsp; max       |209.058254168693      |209.058254168693      |209.058254168693      |209.058254168693      |
#> |**Happiness**          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Happiness |34 (34.00%)           |34 (34.00%)           |34 (34.00%)           |34 (34.00%)           |

To get the correct summary statistics for each city only use the variable name.

summary_statistics <-
  list(
    "Hobby(hours/week)" =
      list(
           "mean (sd)" = ~ qwraps2::mean_sd(hobby_hr_week, na_rm = TRUE),
           "min"       = ~ min(hobby_hr_week, na.rm = TRUE),
           "max"       = ~ max(hobby_hr_week, na.rm = TRUE)
           ),
       "Work(hours/week)" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(work_hr_week, na_rm = TRUE),
              "min"       = ~ min(work_hr_week, na.rm = TRUE),
              "max"       = ~ max(work_hr_week, na.rm = TRUE)
              ),
       "Wellness" =
         list(
              "mean (sd)" = ~ qwraps2::mean_sd(wellness, na_rm = TRUE),
              "min"       = ~ min(wellness, na.rm = TRUE),
              "max"       = ~ max(wellness, na.rm = TRUE)
              ),

       "Happiness" =
         list(
              "Happiness" = ~qwraps2::n_perc(RU_happy)

         )
  )

And the updated table:

print(
      summary_table(myData, summary_statistics, by = "city"),
      rtitle = "Summary Statistics Table for the Wellness Data Set"
)
#> 
#> 
#> |Summary Statistics Table for the Wellness Data Set |Eilat (N = 25)        |Jerusalem (N = 25)     |Metula (N = 25)       |TelAviv (N = 25)      |
#> |:--------------------------------------------------|:---------------------|:----------------------|:---------------------|:---------------------|
#> |**Hobby(hours/week)**                              |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd)                             |16.20 &plusmn; 4.47   |15.96 &plusmn; 4.59    |15.64 &plusmn; 3.68   |15.24 &plusmn; 3.60   |
#> |&nbsp;&nbsp; min                                   |8                     |4                      |10                    |11                    |
#> |&nbsp;&nbsp; max                                   |24                    |27                     |22                    |24                    |
#> |**Work(hours/week)**                               |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd)                             |29.36 &plusmn; 6.75   |31.24 &plusmn; 5.06    |28.72 &plusmn; 5.78   |28.28 &plusmn; 5.05   |
#> |&nbsp;&nbsp; min                                   |20                    |20                     |19                    |21                    |
#> |&nbsp;&nbsp; max                                   |43                    |41                     |40                    |38                    |
#> |**Wellness**                                       |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; mean (sd)                             |-84.07 &plusmn; 94.75 |-51.45 &plusmn; 124.61 |-36.56 &plusmn; 77.73 |-35.28 &plusmn; 83.20 |
#> |&nbsp;&nbsp; min                                   |-232.587140717081     |-391.075901351521      |-184.147379739545     |-172.159781696125     |
#> |&nbsp;&nbsp; max                                   |120.55715927506       |209.058254168693       |113.344403784632      |113.643322269377      |
#> |**Happiness**                                      |&nbsp;&nbsp;          |&nbsp;&nbsp;           |&nbsp;&nbsp;          |&nbsp;&nbsp;          |
#> |&nbsp;&nbsp; Happiness                             |9 (36.00%)            |10 (40.00%)            |9 (36.00%)            |6 (24.00%)            |

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

enter image description here

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)
#>  qwraps2     * 0.5.0   2020-08-31 [1] local         
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5   2020-07-06 [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
  • Question: How do you go from the unfinished ```print( summary_table(myData, summary_statistics, by = "city"), rtitle = "Summary Statistics Table for the Wellness Data Set" )``` to the finished table? Even after knitting, I'm still receiving a table that is similar to your unfinished one. – James Nov 07 '20 at 23:26
  • 1
    @james, my guess is that you need to have the code chunk option `results = 'asis'` set. – Peter Nov 08 '20 at 05:06
  • @Peter...haha, amazing. You were right. Thanks much! – James Nov 08 '20 at 05:44