0

I am using Expss package to create Tables in R. I am have 5 Statements each statement Have 5 brands. 5 Statements are in 5 consecutive variables like a1,a2,a3,a4,a5 Can i have table in grid format like the following?

enter image description here

Divya
  • 47
  • 7

1 Answers1

0

There are two solutions: one is verbose but not scalable, second is scalable but is not very simple. Both solutions are based on the idea that we reposition labels from variables to banner location.

    library(expss)
    # create sample of data
    set.seed(123)
    N = 150
    df = data.frame(
        st1 = sample(paste0("brand", 1:5), N, replace = TRUE),
        st2 = sample(paste0("brand", 1:5), N, replace = TRUE),
        st3 = sample(paste0("brand", 1:5), N, replace = TRUE),
        st4 = sample(paste0("brand", 1:5), N, replace = TRUE),
        st5 = sample(paste0("brand", 1:5), N, replace = TRUE)
    ) %>% apply_labels(
                       st1 = 'Statement 1', 
                       st2 = 'Statement 2', 
                       st3 = 'Statement 3', 
                       st4 = 'Statement 4', 
                       st5 = 'Statement 5' 
                       )

    # verbose solution with Tab_*. It is not scalable for large number of variables 
    # manipulation with variable labels is needed to repostion variable labels from rows to column
    df %>% 
        tab_total_row_position("above") %>% 
        tab_cells("|" = drop_var_labs(st1)) %>%
        tab_stat_cpct(label = var_lab(st1)) %>% 
        tab_cells("|" = drop_var_labs(st2)) %>% 
        tab_stat_cpct(label = var_lab(st2)) %>% 
        tab_cells("|" = drop_var_labs(st3)) %>% 
        tab_stat_cpct(label = var_lab(st3)) %>% 
        tab_cells("|" = drop_var_labs(st4)) %>% 
        tab_stat_cpct(label = var_lab(st4)) %>% 
        tab_cells("|" = drop_var_labs(st5)) %>%
        tab_stat_cpct(label = var_lab(st5)) %>% 
        tab_pivot(stat_position = "inside_columns")  %>% 
        tab_transpose() 

    # solution wich will work for arbirary number of variables
    df %>% 
        calculate(
            lapply(st1 %to% st5, function(item)
                # manipulation with variable labels is needed to repostion variable labels from rows to column
                cro(list(drop_var_labs(item)), list(var_lab(item)), total_row_position = "above")
            )
        ) %>% 
        Reduce("%merge%", .) %>% 
        tab_transpose()
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20