It's pretty common to use %>%
operator in conjuction with .
as a representation of the left-hand-side (LHS) object of %>%
, for example:
library(purrr)
mtcars %>%
split(.$cyl) %>% # as you can see here
map(~ lm(mpg ~ hp, data = .x))
But using rsample::bootstraps()
function to create a tibble with a bootstrap-list-column, where each element have a dataset, I noticed an error using the .
pattern describe above that I don´t understand well.
library(purrr)
# create a 3 partitions
# inspect how many cyl == 4 are in each partition (ERROR)
rsample::bootstraps(mtcars, times = 3) %>%
map_dbl(.$splits,
function(x) {
cyl = as.data.frame(x)$cyl
mean(cyl == 4)
})
Error: Index 1 must have length 1, not 4
Run `rlang::last_error()` to see where the error occurred.
But instead if you store the output of rsample::bootstraps()
in the ex
object and then use map_dbl
, as you can see in the documentation, it works properly.
library(purrr)
# create 3 partitions
ex <- rsample::bootstraps(mtcars, times = 3)
# inspect how many cyl == 4 are in each partition (WORKS OK)
map_dbl(ex$splits,
function(x) {
cyl = as.data.frame(x)$cyl
mean(cyl == 4)
})
[1] 0.50000 0.28125 0.43750
Any idea to understand this behavior between procedures?