I am needing to conduct inferential analysis of a list of 21 countries comparing results (numeric variable) between gender. I have already created a pivot-long dataset with the following variables: Gender, Country, Results (numeric). I am using gtsummary::tbl_strata and gtsummary::tbl_summary. I could not create a nesting to run each country individually. Also, the output has been returning n(%) counts for the countries (table in wide format); calculating the result variable as overall. I have put the tabular structure I want below.
I could even generate individual tables and stack them. However, I would like a more rational strategy.
Code
library(tidyverse)
library(gtsummary)
# dataframe
df <-
data.frame(
Country = c("Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3"),
Gender = c("M", "M", "M",
"W", "W", "W",
"M", "M", "M",
"W", "W", "W"),
Results = c(53, 67, 48,
56, 58, 72,
78, 63, 67,
54,49,62))
df
# Table
Table <- df %>%
select(c('Gender',
'Country',
'Results')) %>%
tbl_strata(
strata = Country,
.tbl_fun =
~.x %>%
tbl_summary(by = Gender,
missing = "no") %>%
bold_labels() %>%
italicize_levels() %>%
italicize_labels())
Table