Pasting text into dplyr mutate

dplyr paste
paste two columns in r
r combine columns into list
combine multiple variables into one in r
collapse multiple columns into one r
r split vector into columns
split one column into two dplyr
dplyr sep

Id like to be able to pass a string of variables to lag into the dplyr mutate function but am having some trouble. For example, this works fine:

 text <- "lag(depth)"
 diamonds %>% mutate_(text)

But this doesn't and gives an error:

 text <- "lag(depth), lag(table)"
 diamonds %>% mutate_(text)

Error in parse(text = x) : <text>:1:11: unexpected ','
1: lag(depth),

This feels like it should be possible. Any help would be greatly appreciated.


You have to concatenate your text:

text <- c("lag(depth)", "lag(table)")

And then use .dots to parse your text:

library(dplyr)

diamonds %>% 
  mutate_(.dots = text)

## A tibble: 53,940 x 12
#   carat cut     color clarity depth table price     x     y     z `lag(depth)`
#   <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>        <dbl>
# 1 0.23  Ideal   E     SI2      61.5    55   326  3.95  3.98  2.43         NA
# 2 0.21  Premium E     SI1      59.8    61   326  3.89  3.84  2.31         61.5
# 3 0.23  Good    E     VS1      56.9    65   327  4.05  4.07  2.31         59.8
# 4 0.290 Premium I     VS2      62.4    58   334  4.2   4.23  2.63         56.9
# 5 0.31  Good    J     SI2      63.3    58   335  4.34  4.35  2.75         62.4
# 6 0.24  Very G~ J     VVS2     62.8    57   336  3.94  3.96  2.48         63.3
# 7 0.24  Very G~ I     VVS1     62.3    57   336  3.95  3.98  2.47         62.8
# 8 0.26  Very G~ H     SI1      61.9    55   337  4.07  4.11  2.53         62.3
# 9 0.22  Fair    E     VS2      65.1    61   337  3.87  3.78  2.49         61.9
#10 0.23  Very G~ H     VS1      59.4    61   338  4     4.05  2.39         65.1
## ... with 53,930 more rows, and 1 more variable: `lag(table)` <dbl>

r - Pasting text into dplyr mutate, You have to concatenate your text: text <- c("lag(depth)", "lag(table)"). And then use .dots to parse your text : library(dplyr) diamonds  Name-value pairs of expressions, each with length 1 or the same length as the number of rows in the group (if using group_by()) or in the entire input (if not using groups). The name of each argument will be the name of a new variable, and the value will be its corresponding value. Use a NULL value in mutate to drop a variable.


as an alternative solution you can try to use mutate_at. The function lag must be duplicated using this approach.

library(tidyverse)
a <- unlist(stringi::stri_extract_all_words(text)) 
diamonds %>% 
  mutate_at(.vars = a[a %in% colnames(diamonds)], .funs = c(lag=a[duplicated(a)]))
# A tibble: 53,940 x 12
   carat cut       color clarity depth table price     x     y     z depth_lag table_lag
   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>     <dbl>     <dbl>
 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43      NA          NA
 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31      61.5        55
 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31      59.8        61
 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63      56.9        65
 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75      62.4        58
 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48      63.3        58
 7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47      62.8        57
 8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53      62.3        57
 9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49      61.9        55
10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39      65.1        61

Unite multiple columns into one by pasting strings - Tidyr, Convenience function to paste together multiple columns into one. unite(data, col​, , sep = "_"  A data expression is either a bare name like x or an expression like x:y or c(x, y). In a data expression, you can only refer to columns from the data frame. Everything else is a context expression in which you can only refer to objects that you have defined with <-.


We could do this with parse_exprs from rlang

library(tidyverse)
library(rlang)
text <- "lag(depth); lag(table)" #note the `;` separation
diamonds %>% 
     mutate(!!! parse_exprs(text))
# A tibble: 53,940 x 12
#   carat cut     color clarity depth table price     x     y     z `lag(depth)`
#   <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>        <dbl>
# 1 0.23  Ideal   E     SI2      61.5    55   326  3.95  3.98  2.43         NA  
# 2 0.21  Premium E     SI1      59.8    61   326  3.89  3.84  2.31         61.5
# 3 0.23  Good    E     VS1      56.9    65   327  4.05  4.07  2.31         59.8
# 4 0.290 Premium I     VS2      62.4    58   334  4.2   4.23  2.63         56.9
# 5 0.31  Good    J     SI2      63.3    58   335  4.34  4.35  2.75         62.4
# 6 0.24  Very G… J     VVS2     62.8    57   336  3.94  3.96  2.48         63.3
# 7 0.24  Very G… I     VVS1     62.3    57   336  3.95  3.98  2.47         62.8
# 8 0.26  Very G… H     SI1      61.9    55   337  4.07  4.11  2.53         62.3
# 9 0.22  Fair    E     VS2      65.1    61   337  3.87  3.78  2.49         61.9
#10 0.23  Very G… H     VS1      59.4    61   338  4     4.05  2.39         65.1
# ... with 53,930 more rows, and 1 more variable: `lag(table)` <dbl>

parse vector of functions in mutate - tidyverse, Passing named list to mutate (and probably other dplyr verbs) tidyverse. Hi, I want to I'm trying to paste together the function and then parse it. Dynamic column/variable names with dplyr using Standard Evaluation functions. Data manipulation works like a charm in R when using a library like dplyr. An often overlooked feature of this library is called Standard Evaluation (SE) which is also described in the vignette about the related Non-standard Evaluation.


Substring duplication for dplyr::mutate · Issue #373 · r-lib/styler · GitHub, After applying styler::style_text(text) over that code, I have it turned into: df <- bind_rows(df_a, df_d) %>% mutate(mutate("POP Name" = paste(symbol, "POP"))​  Pasting text into dplyr mutate. Id like to be able to pass a string of variables to lag into the dplyr mutate function but am having some trouble.


dplyr - mutate: use dynamic variable names, Create a function to dynamically assign variable names: multisepal <- function(df, n) {. varname <- paste("sepal", n , sep=".") mutate(df  Programming recipes. The following recipes walk you through the basics of tidyeval, with the nominal goal of reducing duplication in dplyr code. The examples here are somewhat inauthentic because we’ve reduced them down to very simple components to make them easier to understand.


paste function, paste0(…, collapse) is equivalent to paste(…, sep = "", collapse) , slightly more efficiently. If a value is specified for collapse , the values in the result are then  The scoped variants of mutate () and transmute () make it easy to apply the same transformation to multiple variables. There are three variants: A function fun, a quosure style lambda ~ fun (.) or a list of either form. Additional arguments for the function calls in .funs. These are evaluated only once, with tidy dots support.