Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract element of a list using pipe() function

Tags:

r

dplyr

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?

like image 434
Bogaso Avatar asked Sep 13 '25 09:09

Bogaso


1 Answers

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
like image 154
akrun Avatar answered Sep 14 '25 22:09

akrun