Reputation: 6020
I want to select items by index from a list before applying another function to it using purrr:map. I have tried the following, but can't find a way that works.
require(dplyr)
require(purrr)
dat <- list(1:3,
4:6,
letters[1:3])
# I can select one item
dat[1]
# I can select two items
dat[c(1,2)]
# But how can I do this in a pipeline by index?
dat %>% map(mean)
dat %>%
filter(c(1,2)) %>%
map(mean)
dat %>%
keep(1,2) %>%
map(mean)
dat %>%
select(1,2) %>%
map(mean)
Upvotes: 3
Views: 4592
Reputation: 26343
We can use `[`
and do
dat %>%
.[c(1, 2)] %>%
map(., mean)
#[[1]]
#[1] 2
#[[2]]
#[1] 5
Or define an alias in the way the magrittr
package does it
extract <- `[` # literally the same as magrittr::extract
dat %>%
extract(c(1, 2)) %>%
map(., mean)
Which could also be written as
dat %>% `[`(c(1,2))
Using baseR pipe operator this would read
dat |>
`[`(x = _, j = c(1,2)) |> # R version >= R 4.2.0
lapply(mean)
#[[1]]
#[1] 2
#
#[[2]]
#[1] 5
Upvotes: 11
Reputation: 887231
An option is
library(tidyverse)
keep(dat, seq_along(dat) %in% 1:2) %>%
map(mean)
#[[1]]
#[1] 2
#[[2]]
#[1] 5
Or map
with pluck
map(1:2, ~ pluck(dat, .x) %>%
mean)
Or with assign_in
assign_in(dat, 3, NULL) %>%
map(mean)
Or another option is map_if
map_if(dat, is.numeric, mean, .else = ~ NULL) %>%
discard(is.null)
Or with discard
discard(dat, is.character) %>%
map(mean)
or with Filter
and map
Filter(is.numeric, dat) %>%
map(mean)
NOTE: All of them gets the expected output.
Upvotes: 4