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
Many thanks for your quick reply. Both solutions get rid of the error message, but don't quite produce the intended result, as shown below. Any ideas?
# without all_of adds a new column and doesn't convert the intended column to a factor
# warning message removed
map(vars_list, ~quick_tab(mtcars, .x))
#> [[1]]
#> # A tibble: 4 x 4
#> cyl n percent .x
#> <chr> <dbl> <chr> <fct>
#> 1 4 11 34.4% cyl
#> 2 6 7 21.9% cyl
#> 3 8 14 43.8% cyl
#> 4 Total 32 100.0% cyl
#>
#> [[2]]
#> # A tibble: 3 x 4
#> am n percent .x
#> <chr> <dbl> <chr> <fct>
#> 1 0 19 59.4% am
#> 2 1 13 40.6% am
#> 3 Total 32 100.0% am
#>
#> [[3]]
#> # A tibble: 3 x 4
#> vs n percent .x
#> <chr> <dbl> <chr> <fct>
#> 1 0 18 56.2% vs
#> 2 1 14 43.8% vs
#> 3 Total 32 100.0% vs
# when adding the double bangs I lose the value "labels" for each of the factor levels
map(vars_list, ~quick_tab(mtcars, !!all_of(.x)))
#> [[1]]
#> # A tibble: 4 x 3
#> cyl n percent
#> <fct> <dbl> <chr>
#> 1 cyl 11 34.4%
#> 2 cyl 7 21.9%
#> 3 cyl 14 43.8%
#> 4 cyl 32 100.0%
#>
#> [[2]]
#> # A tibble: 3 x 3
#> am n percent
#> <fct> <dbl> <chr>
#> 1 am 19 59.4%
#> 2 am 13 40.6%
#> 3 am 32 100.0%
#>
#> [[3]]
#> # A tibble: 3 x 3
#> vs n percent
#> <fct> <dbl> <chr>
#> 1 vs 18 56.2%
#> 2 vs 14 43.8%
#> 3 vs 32 100.0%
I'd like the variables/columns (e.g., cyl, am, vs) converted to a factor while preserving the original "labels," as in the below, where cyl is a factor with 4, 6, 8, and Total as levels:
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%