Is there a way I can generate the below cross tab in R?

91 Views Asked by At

[Crostab output from SPSS] enter image description here

I want to generate the attached crosstab with R. Presently I used SPSS. The data frame that I used is below. variables (Maths_Set, Note_Book, School_Bag, Text_Book, Pen ) is a muliple response question in a survey, where respondent can have 1 or more of the items. if 1 is coded, it means the respondent has the item otherwise it's coded 0. The crosstab is taking Q1 as a row and Gender and the grouping of items as a column. And it generates both count and percentage in a single cell.

Q1 <- c(5,5,4,1,5,4,3,3,5,2)
Gender <- c(2,2,2,1,2,2,1,2,1,2)
Maths_Set <- c(1,0,1,1,1,1,1,1,0,1)
Note_Book <- c(1,1,0,1,1,1,1,1,1,1)
School_Bag <- c(0,1,1,0,1,1,1,1,1,1)
Text_Book <- c(0,1,0,1,1,1,1,1,1,1)
Pen <- c(0,0,1,1,1,1,1,1,1,1)

df = data.frame(Q1,Gender,Maths_Set,Note_Book,School_Bag,Text_Book,Pen)
df$Q1 <- factor(df$Q1, levels= c(1,2,3,4,5), labels = c("Strongly Dislike", "Dislike", "Neither like or Dislike", "Like", "Strongly Like"))
df$Gender <- factor(df$Gender, levels = c(1,2), labels = c("Boy", "Girl"))
2

There are 2 best solutions below

0
thothal On

You can use gt for formatting and dplyr for the aggregation:

st <- df %>% 
  group_by(Q1) %>% 
  summarize(`Actual Base` = n(),
            Boy = sum(Gender == "Boy"),
            Girl = sum(Gender == "Girl"),
            across(-Gender, .fn = sum)) %>% 
  mutate(across(-Q1, .fn = list(freq = ~ .x / sum(.x))))

bt <- gt(st,
   rowname_col = "Q1") %>% 
  grand_summary_rows(fns = list("Actual Base" ~ sum(.)), 
                     side = "top") %>% 
  fmt_percent(columns = ends_with("freq"),
              decimals = 1) %>% 
  tab_spanner("Gender", c("Boy", "Girl")) %>% 
  tab_spanner("School Items", 5:9)

Reduce(\(x, y) x %>%
         cols_merge(starts_with(y), pattern = "{1} ({2})"),
       st %>% select(!ends_with("freq"), -Q1) %>% names(),
       init = bt)

This is just to get you started with gt. There are plenty of customization options and feel free to explore them to customize the table to your liking.

Table produced by gt which shows counts and percentages of the table after the aggregation

0
Alex Strashny On

The following creates a series of tables with the information that you are looking for, but it does not replicate the layout of the SPSS crosstab.

The advantage in this method is that the commands are straightforward.

logical_vars = setdiff(names(df), c("Q1", "Gender") )
for (nn in logical_vars) {
  df[,nn] = as.logical(df[,nn])
}
mysurvey = survey::svydesign(ids = ~1, data = df)
#> Warning in svydesign.default(ids = ~1, data = df): No weights or probabilities
#> supplied, assuming equal probability
library(surveytable)
set_survey(mysurvey)
#> * To adjust how counts are rounded, see ?set_count_int
#>                        _                                             
#> Survey name            mysurvey                                      
#> Number of variables    7                                             
#> Number of observations 10                                            
#> Info1                  Independent Sampling design (with replacement)
#> Info2                  survey::svydesign(ids = ~1, data = df)
set_count_int()
#> * Rounding counts to the nearest integer.
#> * ?set_count_int for other options.
options(surveytable.check_present = FALSE)

tab("Q1")
tab_subset("Q1", "Gender")
for (nn in logical_vars) {
  print( tab_subset("Q1", nn, TRUE) )
}

                               Q1 {mysurvey}                                
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬──────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │   LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Strongly       │      1 │  1 │  0 │   Inf │      10 │ 10   │  0.2 │ 47   │
│ Dislike        │        │    │    │       │         │      │      │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │      10 │ 10   │  0.2 │ 47   │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Neither like   │      2 │  1 │  0 │ 9,547 │      20 │ 13.3 │  2.1 │ 57.8 │
│ or Dislike     │        │    │    │       │         │      │      │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Like           │      2 │  1 │  0 │ 9,547 │      20 │ 13.3 │  2.1 │ 57.8 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Strongly Like  │      4 │  2 │  1 │    15 │      40 │ 16.3 │ 11.1 │ 75.5 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴──────┴──────┘

                      Q1 (Gender = Boy) {mysurvey}                       
┌────────────────┬────────┬────┬────┬─────┬─────────┬──────┬─────┬──────┐
│ Level          │ Number │ SE │ LL │  UL │ Percent │   SE │  LL │   UL │
├────────────────┼────────┼────┼────┼─────┼─────────┼──────┼─────┼──────┤
│ Strongly       │      1 │  1 │  0 │ Inf │    33.3 │ 28.7 │ 0.6 │ 92.2 │
│ Dislike        │        │    │    │     │         │      │     │      │
├────────────────┼────────┼────┼────┼─────┼─────────┼──────┼─────┼──────┤
│ Neither like   │      1 │  1 │  0 │ Inf │    33.3 │ 28.7 │ 0.6 │ 92.2 │
│ or Dislike     │        │    │    │     │         │      │     │      │
├────────────────┼────────┼────┼────┼─────┼─────────┼──────┼─────┼──────┤
│ Strongly Like  │      1 │  1 │  0 │ Inf │    33.3 │ 28.7 │ 0.6 │ 92.2 │
└────────────────┴────────┴────┴────┴─────┴─────────┴──────┴─────┴──────┘

                       Q1 (Gender = Girl) {mysurvey}                       
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬─────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │  LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │    14.3 │ 13.9 │ 0.3 │ 60.6 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Neither like   │      1 │  1 │  0 │   Inf │    14.3 │ 13.9 │ 0.3 │ 60.6 │
│ or Dislike     │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Like           │      2 │  1 │  0 │ 9,547 │    28.6 │ 18   │ 3.1 │ 73.2 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly Like  │      3 │  2 │  0 │    27 │    42.9 │ 19.7 │ 8.7 │ 83.3 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴─────┴──────┘

                     Q1 (Maths_Set = TRUE) {mysurvey}                      
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬─────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │  LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly       │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
│ Dislike        │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Neither like   │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
│ or Dislike     │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Like           │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly Like  │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴─────┴──────┘

                      Q1 (Note_Book = TRUE) {mysurvey}                      
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬──────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │   LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Strongly       │      1 │  1 │  0 │   Inf │    11.1 │ 11   │  0.2 │ 50.8 │
│ Dislike        │        │    │    │       │         │      │      │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │    11.1 │ 11   │  0.2 │ 50.8 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Neither like   │      2 │  1 │  0 │ 9,547 │    22.2 │ 14.6 │  2.4 │ 62.2 │
│ or Dislike     │        │    │    │       │         │      │      │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Like           │      1 │  1 │  0 │   Inf │    11.1 │ 11   │  0.2 │ 50.8 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼──────┼──────┤
│ Strongly Like  │      4 │  2 │  1 │    15 │    44.4 │ 17.5 │ 12.5 │ 80.4 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴──────┴──────┘

                     Q1 (School_Bag = TRUE) {mysurvey}                     
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬─────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │  LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Neither like   │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
│ or Dislike     │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Like           │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly Like  │      3 │  2 │  0 │    27 │    37.5 │ 18   │ 7.5 │ 77.4 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴─────┴──────┘

                     Q1 (Text_Book = TRUE) {mysurvey}                      
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬─────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │  LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly       │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
│ Dislike        │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Neither like   │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
│ or Dislike     │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Like           │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly Like  │      3 │  2 │  0 │    27 │    37.5 │ 18   │ 7.5 │ 77.4 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴─────┴──────┘

                        Q1 (Pen = TRUE) {mysurvey}                         
┌────────────────┬────────┬────┬────┬───────┬─────────┬──────┬─────┬──────┐
│ Level          │ Number │ SE │ LL │    UL │ Percent │   SE │  LL │   UL │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly       │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
│ Dislike        │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Dislike        │      1 │  1 │  0 │   Inf │    12.5 │ 12.3 │ 0.2 │ 55.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Neither like   │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
│ or Dislike     │        │    │    │       │         │      │     │      │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Like           │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
├────────────────┼────────┼────┼────┼───────┼─────────┼──────┼─────┼──────┤
│ Strongly Like  │      2 │  1 │  0 │ 9,547 │    25   │ 16.1 │ 2.7 │ 67.3 │
└────────────────┴────────┴────┴────┴───────┴─────────┴──────┴─────┴──────┘