Consider this data that needs the summary measures mean and sd on multiple variables,
# Create grouping var; ####
mtcars <- mtcars %>% mutate(
am = case_when(
am == 0 ~ "Automatic",
TRUE ~ "Manual"
)
)
With the following custom function and purrr, I can create a baseline table,
# Summarising function; ####
sum_foo <- function(data, var) {
data %>%
group_by(am) %>%
summarise(
mean = mean( !!sym(var) , na.rm = TRUE),
sd = sd( !!sym(var) , na.rm = TRUE)
) %>%
mutate(across(where(is.double), round, 2)) %>%
group_by(am) %>%
transmute(
value = paste(mean, "(±", sd, ")", sep = ""),
variable = var
) %>%
pivot_wider(
names_from = "am"
)
}
# Execute Function; ####
sum_variables <- c("mpg", "hp", "disp")
sum_variables %>% map(
sum_foo,
data = mtcars
) %>% reduce(
bind_rows
)
Which gives the following output,
# A tibble: 3 x 3
variable Automatic Manual
<chr> <chr> <chr>
1 mpg 17.15(±3.83) 24.39(±6.17)
2 hp 160.26(±53.91) 126.85(±84.06)
3 disp 290.38(±110.17) 143.53(±87.2)
I want to get the output without using map and reduce, ie. without iterating through the variables with rowwise or map.
I'm looking for an alternative tidyverse-solution!