9.3 purrr
style
mtcars |>
map(head, 20) |> # pull first 20 of each column
map_dbl(mean) |> # mean of each vector
head()
#> mpg cyl disp hp drat wt
#> 20.13000 6.20000 233.93000 136.20000 3.54500 3.39845
An example from tidytuesday
tt <- tidytuesdayR::tt_load("2020-06-30")
# filter data & exclude columns with lost of nulls
list_df <-
map(
.x = tt[1:3],
.f =
~ .x |>
filter(issue <= 152 | issue > 200) |>
mutate(timeframe = ifelse(issue <= 152, "first 5 years", "last 5 years")) |>
select_if(~mean(is.na(.x)) < 0.2)
)
# write to global environment
iwalk(
.x = list_df,
.f = ~ assign(x = .y, value = .x, envir = globalenv())
)