I would like to know how to use purrr::map
where .f
is a composition of two different functions.
First, let's create a list on which to map a composite function:
library(tidyverse)
# create a list
x <- list(mtcars, tibble::as_tibble(iris), c("x", "y", "z"))
# extracting class of objects
purrr::map(.x = x, .f = class)
#> [[1]]
#> [1] "data.frame"
#>
#> [[2]]
#> [1] "tbl_df" "tbl" "data.frame"
#>
#> [[3]]
#> [1] "character"
Now let's say I want to extract the first element of the class
of each element in the list:
# this works but uses `map` twice
purrr::map(.x = x, .f = class) %>%
purrr::map(.x = ., .f = `[[`, i = 1L)
#> [[1]]
#> [1] "data.frame"
#>
#> [[2]]
#> [1] "tbl_df"
#>
#> [[3]]
#> [1] "character"
That works, but I want to avoid using map
twice and would like to compose a function that can extract class and its first element in a single step. So I tried to compose such a function but it doesn't play nicely with map
# error
purrr::map(.x = x, .f = purrr::compose(class, `[[`, i = 1L))
#> Can't convert an integer vector to function
# no error but not the expected output
purrr::map(.x = x, .f = purrr::compose(class, `[[`), i = 1L)
#> [[1]]
#> [1] "numeric"
#>
#> [[2]]
#> [1] "numeric"
#>
#> [[3]]
#> [1] "character"
How can I do this?
map(x, ~ first(class(.x)))
work – akrunpurrr::map(x, purrr::compose(first, class))
orpurrr::map(x, purrr::compose(~.[[1]], class))
. You can't really pass different parameters to different parts of the function in compose from outside the composition. – MrFlickpurrr::map(x, ~class(.x)[[1]])
works as well if I was hell-bent on using[[
. Can you post your answer and I'll accept it. – Indrajeet Patil