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
4
votes
1 answer

dplyr: Cross-tabulation with pipes

There are two questions about dplyr, which in my case are related by the problem I am trying to solve: How can I cross-classify a data_frame using pipes, when trying to pass the resultant of a series of operations to xtabs? The argument of a…
tchakravarty
  • 10,736
  • 12
  • 72
  • 116
4
votes
1 answer

Idiom in magrittr for x <- x %>%

With magrittr (or dplyr), I find myself using the following pattern quite often: x <- x %>% fun %>% fun Is there a commonly used shortcut or idiom for that? by which I mean, an operand, e.g. %^>%, with which one could write: x %^>% fun…
Alexandre Halm
  • 979
  • 1
  • 8
  • 18
4
votes
2 answers

dplyr + magrittr + qplot = no plot?

I want to use qplot (ggplot2) and then forward the data with magrittr: This works: mtcars %>% qplot(mpg, cyl, data=.) This produces an error: mtcars %>% qplot(mpg, cyl, data=.) %>% summarise(mean(mpg)) And those produce only summary…
Tim
  • 7,075
  • 6
  • 29
  • 58
3
votes
1 answer

What the magrittr pipe %>% does in the shadows

Those two functions don't give the same output, because of how works the %>% operator, but I can't understand why? my_function <- function(x) { # Extract the name of x the_name_of_x <- deparse(substitute(x)) …
Greg Mansio
  • 109
  • 5
3
votes
1 answer

Sorting column names by two numbers

I recently got this amazing answer from JBGruber, to order string columns with double numerical values, which works on both datasets at the bottom of the post: library(magrittr) order_cols <- function(dat) { # look for words to order by …
Tom
  • 2,173
  • 1
  • 17
  • 44
3
votes
1 answer

Cut numeric variable in data frame with different breaks according to group (preferably with dplyr)

I've tried to find a solution to this here, but nothing seems to address exactly my case. Sorry if I missed it. I have a data frame with each row providing a position in one of various categories (e.g., row one corresponds position 2 within…
3
votes
5 answers

Count the number of non-zero elements of each column- Piping Friendly

I am a behavioral ecologist using R. I am trying to count the non-zero elements across several columns. Normally when I do this I have successfully employed colSums with the !=0 operator, as has been suggested in many posts here and elsewhere…
N.F.F.
  • 65
  • 6
3
votes
0 answers

What are the differences between R's new native pipe `|>` and the old native pipe `->.;`?

In R 4.1 a native pipe operator was introduced that is "more streamlined" than previous implementations. See this post about it. This is a question inspired by the thread "What are the differences between R's new native pipe |> and the magrittr pipe…
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
3
votes
1 answer

Inconsistent assign() behavior in simple piping with Tidyverse

By simply changing the argument order in the join step, I can get the code below to run. I just installed the most recent version of Tidyverse as of this post (1.3.1), and I'm using R version 4.1.1 (2021-08-10), "Kick Things". Please end my…
Aegis
  • 145
  • 10
3
votes
2 answers

Access result later in pipe

Access result later in pipe I am trying to create functions which print the number of rows excluded in a dataset at each step in a pipe. Something like this: iris %>% function_which_save_nrows_and_return_the_data() %>% filter(exclude some…
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
3
votes
3 answers

Why does running `f <- function(x){x} %>% f` not throw an error?

R does not complain when one runs: library(magrittr) f <- function(x){x} %>% f (but running f(1) throws a C stack error). However, isn't it equivalent to: f <- f(function(x){x}) which throws the error f not found? Why does the first command not…
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
3
votes
3 answers

Why does the colnames function generate a tibble (and not a vector), when used with the pipe operator and [ selector?

Question: Why does the colnames function generate a tibble, when used with the pipe operator %>% and the [ selector? Example: Given the following tibble: library(tidyverse) x <- tribble( ~a, ~b, 1, 0, 0, 1 ) Problem: The colnames function…
thando
  • 483
  • 2
  • 4
  • 18
3
votes
1 answer

How to `list2env()` (or pipe into `assign()`) within a function call?

I have a use-case for mapping a function to a vector and then assigning the results to individual objects in the parent environment - not the global environment, but the environment that the map() was called from. Specifically, this is all happening…
DHW
  • 1,157
  • 1
  • 9
  • 24
3
votes
2 answers

Sum NA across specific columns in R

I have data such as this: data_in <- read_table2("Id Q62_1 Q62_2 Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 1 Yes Sometimes 2 Always 3 4 No Always Yes 5 …
NewBee
  • 990
  • 1
  • 7
  • 26
3
votes
2 answers

magrittr tee pipe %T>% equivalent

I'm looking at the example of the magrittr tee pipe %T>% in the documentation, but I'm not immediately grasping what it's doing. Can someone show what the equivalent code is without the tee pipe? rnorm(200) %>% matrix(ncol = 2) %T>% plot %>% # plot…
Arthur Yip
  • 5,810
  • 2
  • 31
  • 50