Hello,
I'm having trouble using a function that includes mutate with the map or walk functions. As shown below, the function works on a single column in a dataframe, but when I attempt to apply the function to multiple columns using map, I get the error shown below. Any help to successfully apply the function to multiple columns would be greatly appreciated.
suppressWarnings(suppressPackageStartupMessages({
library(tidyverse)
library(janitor)
}))
# tabulation function
quick_tab <- function(df, x) {
obj <- df %>%
tabyl({{x}}) %>%
adorn_totals() %>%
adorn_pct_formatting() %>%
mutate({{x}} := as_factor({{x}})) %>%
as_tibble()
obj
}
# works as intended with a single column
mtcars %>%
quick_tab(cyl)
#> # A tibble: 4 x 3
#> cyl n percent
#> <fct> <dbl> <chr>
#> 1 4 11 34.4%
#> 2 6 7 21.9%
#> 3 8 14 43.8%
#> 4 Total 32 100.0%
# variable list to apply function to
vars_list <- c("cyl", "am", "vs")
# using map produces this error
map(vars_list, ~quick_tab(mtcars, all_of(.x)))
#> Error: The LHS of `:=` must be a string or a symbol
Created on 2021-11-23 by the reprex package (v2.0.0)