Search code examples
rexpss

Top 3 Box without TRUE FALSE in R / Rstudio


I'm new to R. I'm able to create top 3 and bottom 3 boxes in my tables, but it displays as "TRUE" and "FALSE" like this...

enter image description here

The code that i used is...

library(expss)

X4607 %>%
tab_cells(qcs1a_SQ001, "Top 3 Box"=qcs1a_SQ001>7 & qcs1a_SQ001<11, "Bottom 3 Box"=qcs1a_SQ001<=2) %>%
tab_cols(total(), spcode) %>%
tab_stat_cpct() %>%
tab_last_sig_cpct() %>%
tab_pivot()

Is there any way to just have the number of 'TRUE' come in under the "top 3 box" label and get rid of the "TRUE" and "FALSE" displaying.


Solution

  • There is a special function subtotal for that:

    library(expss)
    
    set.seed(123)
    N = 100
    X4607 = data.frame(
        spcode = sample(c("South", "North"), size = N, replace = TRUE),
        qcs1a_SQ001 = sample(c(1:10, 99), size = N, replace = TRUE)
        
        )
    
    X4607 %>%
        tab_cells(subtotal(qcs1a_SQ001, "Bottom 3 Box" = 1:3, "Top 3 Box" = 7:10, position = "bottom")) %>%
        tab_cols(total(), spcode) %>%
        tab_stat_cpct() %>%
        tab_last_sig_cpct() %>%
        tab_pivot()
    
    # |              | #Total | spcode |        |
    # |              |        |  North |  South |
    # |              |        |      A |      B |
    # | ------------ | ------ | ------ | ------ |
    # |            1 |    5.0 |    9.3 |  1.8   |
    # |            2 |    5.0 |    4.7 |  5.3   |
    # |            3 |    9.0 |    4.7 | 12.3   |
    # |            4 |   11.0 |    7.0 | 14.0   |
    # |            5 |    6.0 |    9.3 |  3.5   |
    # |            6 |   10.0 |    2.3 | 15.8 A |
    # |            7 |   13.0 |   16.3 | 10.5   |
    # |            8 |   14.0 |   16.3 | 12.3   |
    # |            9 |   10.0 |   11.6 |  8.8   |
    # |           10 |   10.0 |    9.3 | 10.5   |
    # |           99 |    7.0 |    9.3 |  5.3   |
    # | Bottom 3 Box |   19.0 |   18.6 | 19.3   |
    # |    Top 3 Box |   47.0 |   53.5 | 42.1   |
    # | #Total cases |    100 |     43 |   57   |