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

How to rename column based on value of the first row of another column in dplyr pipe

I have a long pipe of different filtering and selecting functions, and in that same pipe operation, I would like to rename a column based on the value in the first row of another column. I have to do this for many different data frames, so a…
L Smeets
  • 888
  • 4
  • 17
3
votes
2 answers

R select items from list in pipeline

I want to select items by index from a list before applying another function to it using purrr:map. I have tried the following, but can't find a way that works. require(dplyr) require(purrr) dat <- list(1:3, 4:6, letters[1:3]) # I can…
Wietze314
  • 5,942
  • 2
  • 21
  • 40
3
votes
1 answer

dplyr pipe input processing?

I have not been able to find an answer to this question, so I want to pose it here. Does anyone know what is going on here? as.integer(.29*100) [1] 28 .29*100 %>% as.integer [1] 29 I understand that .29*100 would be a double and doubles cannot be…
John Snyder
  • 133
  • 3
3
votes
1 answer

Filtering rows of a character vector using stringr's str_detect()

I'm trying to subset a character column a using dplyr::filter(), stringr:: str_detect and the magrittr-pipe using a regular expression capturing the presence of two or more digits. This only seems to work for a numerical column, and only when…
Balthasar
  • 197
  • 2
  • 14
3
votes
1 answer

Error object "." not found when using pipe

I'm sorry if this is a repeat but I can't find it: I'm trying to use the . placeholder with the pipe (%>%) from magrittr, and it seems not to work when it's the second call. For instance, this works: data.frame(t = c(1.1,2.2,3.3), y = c(1,2,3))…
Danny
  • 383
  • 2
  • 3
  • 16
3
votes
1 answer

Making a function that takes a function call as an argument into a pipeable function.

require(magrittr) require(purrr) is.out.same <- function(.call, ...) { ## Checks if args in .call will produce identical output in other functions call <- substitute(.call) # Captures function call f_names <-…
3
votes
1 answer

How shall I properly address the pipe?

Trying to make magrittr pinping function more graceful and readable. However, the best I can do about this is as following. How can I improve it? Please find the referential code, and advise. Thanks DF <- data.frame(a=letters[1:10], b=1L:10L,…
Grec001
  • 1,111
  • 6
  • 20
3
votes
4 answers

R - Nested list to tibble

I have a nested list like so: > ex <- list(list(c("This", "is", "an", "example", "."), c("I", "really", "hate", "examples", ".")), list(c("How", "do", "you", "feel", "about", "examples", "?"))) > ex [[1]] [[1]][[1]] [1] "This" "is" "an" …
Christopher Costello
  • 1,186
  • 2
  • 16
  • 30
3
votes
2 answers

Gather() list columns to rows in R

I want to gather() list columns to create new rows in my data frame. I'm using the Game of Thrones data set in the repurrrsive package. Below is my code to set up the problem: library(tidyverse) got_chars <- repurrrsive::got_chars df <-…
Mark Druffel
  • 629
  • 4
  • 10
3
votes
2 answers

R package not loading `Imports` packages

I am working on a package which has a function that relies on dplyr among many other packages. As suggested by H. Wickham in his R Packages book, I am including all the necessary packages under Imports in the Description file. Imports: …
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
3
votes
3 answers

Print data frame dimensions at each step of filtering

I am using the tidyverse to filter out a dataframe and would like a print at each step of the dimensions (or nrows) of the intermediate objects. I thought I could simply use a tee pipe operator from magrittr but it doesn't work. I think I understand…
Carpatorus
  • 127
  • 7
3
votes
1 answer

dplyr piping produces different results than base R syntax utilizing same code

I'm performing an academic exercise that demonstrates an issue with my understanding of dplyr. Let me reconstruct the iris data set using base-R syntax: library(dplyr) bind_cols(iris[1], iris[-1]) OK, great that works. Now, I'll pipe everything…
stackinator
  • 5,429
  • 8
  • 43
  • 84
3
votes
2 answers

MagrittR T-pipe for the summary.glm function. Simple issue

I'm actually running on a problem with the T pipe. I'm trying to do 3 things in the same chain: Fit my GLM Save it in a variable Print it's summary So I'm trying the following syntax: my_variable <- data %>% glm(Responce ~ Variables, family)…
lrnv
  • 1,038
  • 8
  • 19
3
votes
4 answers

How to feed pipe into an inequality?

This has come up in multiple instances, and I do not know if this current instance is generalizable to the many cases where this arises for me, but I'm hoping an answer may shed some light. The simplest version of this is when I am doing some data…
Brandon
  • 1,722
  • 1
  • 19
  • 32
3
votes
1 answer

Adding a column to custom piped function

I am new to making functions that take advantage of %>% in R. Given the following data sim <- tribble(~x,~n,1,1,1,2,1,3) I would like to make a function that adds a column like so >sim <- sim %>% mutate(sum = x+n) >sim # A tibble: 3 x 3 x …
Alex
  • 2,603
  • 4
  • 40
  • 73