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.

477 questions
203
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
  • 2,433
  • 4
  • 17
  • 18
202
votes
6 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
  • 4,635
  • 2
  • 26
  • 35
145
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
  • 1,453
  • 2
  • 9
  • 5
136
votes
6 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) %>%…
mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
133
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,415
  • 2
  • 10
  • 7
125
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
  • 4,035
  • 2
  • 34
  • 49
96
votes
5 answers

What are the differences between R's new native pipe `|>` and the magrittr pipe `%>%`?

In R 4.1 a native pipe operator was introduced that is "more streamlined" than previous implementations. I already noticed one difference between the native |> and the magrittr pipe %>%, namely 2 %>% sqrt works but 2 |> sqrt doesn't and has to be…
sieste
  • 8,296
  • 3
  • 33
  • 48
96
votes
4 answers

Use pipe operator %>% 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
  • 2,122
  • 3
  • 15
  • 31
48
votes
4 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
  • 8,069
  • 5
  • 53
  • 102
39
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
  • 176,377
  • 47
  • 447
  • 496
37
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
  • 14,043
  • 5
  • 40
  • 51
36
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,995
  • 21
  • 35
35
votes
5 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
  • 4,134
  • 1
  • 22
  • 34
31
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
  • 4,052
  • 9
  • 36
  • 54
29
votes
3 answers

How to set the row names of a data frame passed on with the pipe %>% operator?

I have a data frame which I am dcasting using the reshape2 package, and I would like to remove the first column and have it become the row names of the data frame instead. Original dataframe, before dcast: > corner(df) ID_full gene cpm 1 …
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
1
2 3
31 32