What is the correct way to apply a function iteratively over a list?

Meaning, if I have a data frame (i.e. tibble), and I want to

- count the number of observations for specific criteria
- for multiple variables

I was trying to write a `function`

and then use `purrr::map`

to apply it.

```
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.2.2
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
sample_data <- tibble(
year = sample(
x = seq(2011,2020,1),
size = 100,
replace = TRUE
),
var1 = sample(
x = c("1", "0"),
size = 100,
replace = TRUE
),
var2 = sample(
x = c("a", "b"),
size = 100,
replace = TRUE
),
var3 = sample(
x = c("blue", "yellow"),
size = 100,
replace = TRUE
)
)
sample_list <- c(
"var1",
"var2",
"var3"
)
sample_function <- function(x){
sample_data %>%
tabyl(year, x)
}
# apply the function that counts the variable values for each year,
# for each variable in my list
map(
.x = sample_list,
.f = sample_function(x)
)
#> Error in dplyr::select(dat, !!var1, !!var2): object 'x' not found
```

^{Created on 2023-02-14 with reprex v2.0.2}