Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe a data frame to a function whose argument pipes a dot

How can one pipe a data frame to a function whose argument pipes a dot?

mpg %>% rbind(., . %>% rev())

Error in rep(xi, length.out = nvar) : attempt to replicate an object of type 'closure'

Another example:

mpg %>%
  {
    . %>% arrange(manufacturer)
  }

Functional sequence with the following components:

  1. arrange(., manufacturer)

Use 'functions' to extract the individual functions.

like image 849
Edward R. Mazurek Avatar asked Jun 10 '16 21:06

Edward R. Mazurek


People also ask

How does the %>% pipe work in R?

What does the pipe do? The pipe operator, written as %>% , has been a longstanding feature of the magrittr package for R. It takes the output of one function and passes it into another function as an argument. This allows us to link a sequence of analysis steps.

What does it mean to pipe a function?

A pipe function is a function that accepts a series of functions, which process an input parameter and return a output which will be the input for the next function.

Can you pipe in a function R?

And since R is a functional programming language, meaning that everything you do is basically built on functions, you can use the pipe operator to feed into just about any argument call. For example, we can pipe into a linear regression function and then get the summary of the regression parameters.


1 Answers

Wrap the dot to be piped in parentheses like (.):

mpg %>% rbind(., (.) %>% rev())

Or, for lambda function:

mpg %>%
  {
    (.) %>% arrange(manufacturer)
  }
like image 158
Edward R. Mazurek Avatar answered Sep 18 '22 23:09

Edward R. Mazurek