Questions tagged [magrittr]

The magrittr package provides operators for chaining R expressions with forward pipes. Do not use this tag for questions which merely contain a pipe operator. Use this tag for questions asking specifically about the behavior of the %>%, %<>%, %$%, or %T>% operators or the convenience functions provided by the package.

To archive its humble aims, magrittr provides a new “pipe”-like operator for , %>%, with which you may pipe a value forward into an expression or function call; something along the lines of x %>% f, rather than f(x). This is not an unknown feature elsewhere; a prime example is the |> operator used extensively in (to say the least) and indeed this – along with Unix pipes – served as a motivation for developing the magrittr package.

In order to get better answers always use this tag together with .

Development version of magrittr is hosted on GitHub.

396 questions
162
votes
1 answer

What does %>% mean in R

I am following this example, the server.R, file is here. I plan to do a similar filter, but am lost as to what %>% does. # Apply filters m <- all_movies %>% filter( Reviews >= reviews, Oscars >= oscars, Year >=…
ben_says
  • 1,973
  • 4
  • 15
  • 17
147
votes
5 answers

What does %>% function mean in R?

I have seen the use of %>% (percent greater than percent) function in some packages like dplyr and rvest. What does it mean? Is it a way to write closure blocks in R?
alfakini
  • 3,965
  • 2
  • 23
  • 34
114
votes
4 answers

R: use magrittr pipe operator in self written package

I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function…
alexander keth
  • 1,225
  • 2
  • 9
  • 7
107
votes
7 answers

filter for complete cases in data.frame using dplyr (case-wise deletion)

Is it possible to filter a data.frame for complete cases using dplyr? complete.cases with a list of all variables works, of course. But that is a) verbose when there are a lot of variables and b) impossible when the variable names are not known…
user2503795
  • 3,765
  • 2
  • 28
  • 48
98
votes
5 answers

R Conditional evaluation when using the pipe operator %>%

When using the pipe operator %>% with packages such as dplyr, ggvis, dycharts, etc, how do I do a step conditionally? For example; step_1 %>% step_2 %>% if(condition) step_3 These approaches don't seem to work: step_1 %>% step_2 if(condition) %>%…
rmf
  • 8,389
  • 10
  • 46
  • 89
87
votes
5 answers

Error: could not find function "%>%"

I'm running an example in R, going through the steps and everything is working so far except for this code produces an error: words <- dtm %>% as.matrix %>% colnames %>% (function(x) x[nchar(x) < 20]) Error: could not find function "%>%" I…
Haidar
  • 873
  • 1
  • 6
  • 5
78
votes
3 answers

use %>% with replacement functions like colnames()<-

How can I use the pipe operator to pipe into replacement function like colnames()<- ? Here's what I'm trying to do: library(dplyr) averages_df <- group_by(mtcars, cyl) %>% summarise(mean(disp), mean(hp)) colnames(averages_df) <- c("cyl",…
Alex Coppock
  • 1,767
  • 3
  • 13
  • 25
35
votes
2 answers

Chain arithmetic operators in dplyr with %>% pipe

I would like to understand why, in the the dplyr or magrittr package, and more specifically the chaining function %>% has some trouble with the basic operators +, -, *, and / Chaining takes the output of previous statement and feeds it as first…
agenis
  • 6,725
  • 4
  • 34
  • 83
35
votes
4 answers

%>% key binding / keyboard shortcut in Rstudio

I've been experimenting quite a bit with the increasingly popular %>% operator from the magrittr package. I've used it enough that I've set a keyboard shortcut to save me typing: shift+command+. instead of space, shift+5, shift+., shift+5,…
npjc
  • 3,938
  • 1
  • 20
  • 34
34
votes
3 answers

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

Since the introduction of the %>% operator in the magrittr package (and it's use in dplyr), I have started to use this in my own work. One simple operation has me stumped, however. Specifically, this is the extraction (or subsetting) of elements…
Andrie
  • 163,419
  • 39
  • 422
  • 472
31
votes
1 answer

Using the %>% pipe, and dot (.) notation

When using map on a nested data_frame, I do not understand why the latter two version give an error, how should I use the dot (.)? library(tidyverse) # dummy data df <- tibble(id = rep(1:10, each = 10), val = runif(100)) df <-…
johannes
  • 12,861
  • 5
  • 37
  • 49
25
votes
5 answers

Dplyr or Magrittr - tolower?

Is it possible to set all column names to upper or lower within a dplyr or magrittr chain? In the example below I load the data and then, using a magrittr pipe, chain it through to my dplyr mutations. In the 4th line I use the tolower function , but…
RDJ
  • 3,182
  • 6
  • 31
  • 50
24
votes
3 answers

Should I avoid programming packages with pipe operators?

Are there any objective reasons for why pipe operators from the R package magrittr, such as %>%, should be avoided when I program packages in R? More specifically, I want to know if using pipe operators might cause coding conflicts or (positively or…
Johan Larsson
  • 3,119
  • 12
  • 29
23
votes
1 answer

Order of operations in summarise

What is happening in the first line of code and why does the result differ from the two next results? library(tidyverse) library(magrittr) data.frame(A=c(2,2),B=c(1,1)) %>% summarise(A = sum(A),B = sum(B), D=sum(A)-sum(B)) yields…
Martin
  • 331
  • 2
  • 9
23
votes
2 answers

How do I access the data frame that has been passed to ggplot()?

I want to set the string N=xxx as the title of my figure, where xxx is the number of observations in the data frame that I pass as the data argument to ggplot(). In my current code, I explicitly pass that data frame a second time as an argument to…
Schmuddi
  • 1,678
  • 14
  • 30
1
2 3
26 27