'Error in creating a stub in a gt table in R - causing error

I am trying to adjust a gt table employtable1 and create a stub with the various Job roles. For some reason I'm getting an error when running the command and I'm not too sure why or how to fix it. I'm sure it's a fairly simple fix and I'm a novice with R, but would appreciate any help in solving this. I have attached a link with a picture of the current table output and variable names below.

gt table output

This is what the output looks like with the error as well.

> employtable1
> employtable1 <-
+   employtable1 %>%
+   gt(rowname_col = "job") %>%
+   tab_stubhead(label = "Employment Status")
Error in UseMethod("group_vars") : 
  no applicable method for 'group_vars' applied to an object of class "c('gt_tbl', 'list')"

Thanks in advance for your help!



Solution 1:[1]

Its hard to see given you haven't provided the structure of how you made your data frame. What I assume happened given your error code is that however you made your data, it is not in dataframe or table form, which you need to run a gt table. Here I have reconstructed the data in your picture into a data frame which will work:

# Load libraries:
library(gt)

# Create data frame from scratch:
job <- c("Working During the Pandemic",
         "Not Working During the Pandemic",
         "Job Affected by the Pandemic",
         "Student",
         "None of the Above",
         "N/A")

concern_infect_me <- c(1.00,
                       1.26,
                       1.07,
                       1.20,
                       0.75,
                       1.68)
concern_infect_else <- c(1.00,
                         1.21,
                         1.00,
                         1.32,
                         0.89,
                         1.57)
concern_discrim <- c(1.00,
                     1.32,
                     0.86,
                     2.52,
                     0.83,
                     1.43)
concern_finance <- c(1.00,
                     0.89,
                     0.59,
                     3.20,
                     0.90,
                     1.31)
concern_econ <- c(1.00,
                  1.18,
                  0.82,
                  0.97,
                  0.62,
                  1.09)
concern_govt <- c(1.00,
                  0.97,
                  0.99,
                  0.93,
                  0.69,
                  1.49)
employtable1 <- data.frame(job,
           concern_infect_me,
           concern_infect_else,
           concern_discrim,
           concern_finance,
           concern_econ,
           concern_govt)

Then I just did the same thing with your code but changed it to a new table, employtable2:

# Create gt table:
employtable2 <-
  employtable1 %>%
  gt(rowname_col = "job") %>%
  tab_stubhead(label = "Employment Status")

# Print table:
employtable2

Which creates this table:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shawn Hemelstrand