Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse parts of long chain of pipe operators in R?

Tags:

r

dplyr

I have a set of chains of pipe operators (%>%) doing different things with different datasets.

For instance:

dataset %>%
   mutate(...) %>%
   filter(...) %>%
   rowwise() %>%
   summarise() %>%
   etc...

If I want to reuse some parts of these chains, is there a way to do it, without just wrapping it into a function? For instance (in pseudocode obviously):

subchain <- filter(...) %>%
   rowwise() %>%
   summarise() 

# and then instead of the chain above it would be:
dataset %>%
   mutate(...) %>%
   subchain() %>%
   etc...

like image 360
Philipp Chapkovski Avatar asked Jan 04 '21 08:01

Philipp Chapkovski


1 Answers

Similar in syntax to desired pseudo-code:

library(dplyr)

subchain <- . %>% 
  filter(mass > mean(mass, na.rm = TRUE)) %>% 
  select(name, gender, homeworld)

all.equal(
  starwars %>% 
    group_by(gender) %>% 
    filter(mass > mean(mass, na.rm = TRUE)) %>% 
    select(name, gender, homeworld),
  starwars %>% 
    group_by(gender) %>% 
    subchain()
)

Using a dot . as start of a piping sequence. This is in effect close to function wrapping, but this is called a magrittr functional sequence. See ?functions and try magrittr::functions(subchain)

like image 81
Aurèle Avatar answered Nov 14 '22 22:11

Aurèle