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

Piping trimws and gsub in R

I am able to trim out white space and punctuation in R using the below nested functions: x <- " a1~!@#$%^&*(){}_+:\"<>?,./;'[]-= " y <- trimws(gsub("[[:punct:]]", "", x)) #returns "a1" as desired But I'd like to make use of the magrittr /…
SFarkas
  • 3
  • 4
0
votes
1 answer

Conditional dataframe mutations

I have a large data frame that has a lot of string values that I want to clean. Example: students <- data.frame(name = c("John", "Jerry", "Bill", "Tom", "Mary", "Bill"), class = c("A", "B", "-", "#NA", "A", "low"),…
tonyk
  • 348
  • 5
  • 22
0
votes
1 answer

After reading multiple files into R, how can I set the resulting df's to the file name?

Currently I am using the below to read in ~7-10 files to the R console all at once. library(magrittr) library(feather) list.files("C:/path/to/files",pattern="\\.feather$") %>% lapply(read_feather) How can I pipe these into independent data objects…
sgdata
  • 2,543
  • 1
  • 19
  • 44
0
votes
1 answer

Unable to loop using over ARMA models using magrittr forward pipe

I have the following piece of code. It seems very simple it just loops over various combinations of ARMA models AR(1), MA(0) then AR(1) MA(2) and so on. load.or.install <- function(package.names) { is.installed <- function(mypkg)…
Chinwobble
  • 624
  • 1
  • 7
  • 15
0
votes
2 answers

Apply a function references by string name in magrittr

I have a data frame that contains a column with the name of a function I would like to apply to a partition of that data defined by a grouping. Here's a simple example of the set-up: df <- data.frame(x = c(1,2,3,4)) df.with.method <- df %>%…
John Horton
  • 4,122
  • 6
  • 31
  • 45
0
votes
2 answers

Conditionally replace values using magrittr

Suppose I have a dataframe I would like to perform transformations on. Normally it would look like: a <- data.frame(c(NA, 0,1), c(34,NA,0), c(3,9,NA) ) b <- c('key1', 'key2', 'key3') ####replace NA values with 0 a[is.na(a)] <- 0 ####replace 1 with…
DaReal
  • 597
  • 3
  • 10
  • 24
0
votes
0 answers

How to properly use %>% in a package

I'm creating a package, and I'm trying to be smart about the namespace. I also heavily use dplyr for just about everything these days. I understand that I should use: dplyr::select() However, I'm not sure what I need to do to use the piping…
Kyle Ward
  • 889
  • 1
  • 8
  • 18
0
votes
1 answer

pipe followed by .$var into function, why doesnt this work?

Can someone tell me why example 1 is not working but example 2 works? (I pipe a data set into mean function, but want only one variable, but if I do the selection first and pipe the result there is no problem) iris %>% mean(.$Sepal.Length) NA …
Roel Hogervorst
  • 299
  • 3
  • 13
0
votes
1 answer

Conditionally applying functions to grouped dataframes in R with magrittr, dplyr and purrr

I would like to use the succinctness of magrittr, dplyr and possibly purrr to split a large dataframe (with many variables of different types) by one variable x and then apply different functions conditionally by x to each group and row within a…
0
votes
1 answer

Why can ifelse receive only one argument when used with %>%

Normally ifelse requires 3 arguments (test, yes, no): ifelse(c(1,2) == 1, T, F) Providing only a test argument results in an error (because there is no default yes or no field): ifelse(c(1,2) == 1) When used in magrittr, ifelse works fine when…
shackett
  • 323
  • 2
  • 5
0
votes
1 answer

rvest scrape multiple values per node

Taking this example XML Peter Paul Claudia Question:…
Rentrop
  • 20,979
  • 10
  • 72
  • 100
0
votes
1 answer

Match period with Regex in R

A trivial regex operation in R, but even the other questions on SO which appear relevant aren't helping me I have a list of csv files names-- library(plyr) library(stringr) library(magrittr) set.seed("43212") foo <- c(raply(3, …
tomw
  • 3,114
  • 4
  • 29
  • 51
0
votes
1 answer

What is wrong with this line of code with pipe %>%?

I'm trying to run. 1:10 %>% replace(`>`(.,4), 999) Error in replace(`1:10`, . > 4, 999) : object '.' not found Maybe I've been using . wrong all this time, so I go read the magrittr documentation. I learned about using {} to override passing . as…
Vlo
  • 3,168
  • 13
  • 27
0
votes
3 answers

how to write the following code using %>% in R

I am trying to use more and more of the %>% operator with dplyr in my code but I find I am not able figure out how to use %>% all the time. E.g., how would I use it with the complete.cases() function in a correct way for X <- X[complete.cases(X),…
Satya
  • 1,708
  • 1
  • 15
  • 39
0
votes
1 answer

How to write a subsetting operation in magrittr/dplyr: x[!is.na(x)]

I am trying to convert the following idiom to use it in a magrittr functional sequence: x[!is.na(x)] x is any vector. Update: x %>% extract(!is.na(.)) That one is close, but still the operations ! and is.na are not used in functional sequence. I…
Mert Nuhoglu
  • 9,695
  • 16
  • 79
  • 117
1 2 3
31
32