How to extract / subset an element from a list with the magrittr %>% pipe?

Use use_series, extract2 and extract for $, [[, [, respectively.

?extract

magrittr provides a series of aliases which can be more pleasant to use when composing chains using the %>% operator."

For your example, you could try

iris %>%
  extract("Species")

and

iris %>%
  extract2("Species") %>%
  levels

See the bottom of this page for more: http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html


In v 1.5 of magrittr on CRAN you can use the %$% operator:

iris %$% 
  Species %>%
  levels

It is essentially a wrapper around with but nicer than

iris %>% 
  with(Species %>% levels)

or

iris %>%
  with(Species) %>%
  levels

It is designed to be convinient when functions don't have their own data argument, e.g. with plot you can do

iris %>% 
  plot(Sepal.Length ~ Sepal.Width, data = .)

but e.g. with ts.plot you can't do that, so now:

iris %$%
  ts.plot(Sepal.Length)

[yeah, I know the example makes no sense, but it illustrates the point]

Note also that [<- and [[<- also have aliases, inset and inset2..

Tags:

R

Magrittr