Bogaso
Bogaso

Reputation: 3308

Extract element of a list using pipe() function

I want to extract an element of a list using pipe() function and called by name or element number.

MyList = list('a' = 4, 'b' = 19)

Typically we would use below standard syntax -

MyList[['a']]

or

MyList[[1]]

So if I want to use dplyr::pipe() then what is the way?

Upvotes: 4

Views: 972

Answers (1)

akrun
akrun

Reputation: 887098

We can use pluck

library(purrr)
library(magrittr)
MyList %>% 
     pluck('a')
#[1] 4

Or

MyList %>% 
    pluck(1)
#[1] 4

Or use the .$ or .[[

MyList %>% 
     .$a
#[1] 4

Or with extract2 from magrittr

MyList %>% 
   magrittr::extract2('a')
#[1] 4

Upvotes: 5

Related Questions