pass a list to a function

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

  1. count the number of observations for specific criteria
  2. 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

This seems to get you to your desired result. Anytime I use rlang, I'm not confident I'm using it correctly.

library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(rlang)
#> 
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#> 
#>     %@%, as_function, flatten, flatten_chr, flatten_dbl, flatten_int,
#>     flatten_lgl, flatten_raw, invoke, splice

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, !!sym(x))
}

Tabs <- map(
  .x = sample_list,
  .f = ~sample_function(.x)
)
Tabs[[1]]
#>  year 0 1
#>  2011 5 7
#>  2012 8 3
#>  2013 3 6
#>  2014 3 4
#>  2015 6 6
#>  2016 5 3
#>  2017 4 4
#>  2018 5 6
#>  2019 5 3
#>  2020 7 7

Created on 2023-02-14 with reprex v2.0.2

1 Like

Meaning, that my error was a result of data masking? Thanks for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.