1

I want to dynamically mutate a data set using an arithmetical expression in R. I tried following without success.Appreciate some help.

newCol <- 'newCol=gear+wt'

mutate(mtcars,!!newCol) ## This inserted a column newCol=gear+wt

mutate(mtcars,!!sym(newCol)) ## THis gives an error 

'Error in mutate_impl(.data, dots) : Binding not found: newCol=gear+wt.'

JJJ
  • 1,009
  • 6
  • 19
  • 31

3 Answers3

0

You don't use <- when entering a function argument. in dplyr, mutate is a function that lets you create new columns based on any calculation. Here's an example:

library(dplyr)
dataset <- mtcars
dataset %>% mutate(newCol = gear+wt) 

No need for sym here. Hope that's what you're looking for.

Omri374
  • 2,555
  • 3
  • 26
  • 40
0

This is a problem of non-standard evaluation of dplyr.

Quote your expression first, and then unquote.

newCol <- quo(gear + wt)

dplyr::mutate(mtcars, !! newCol)

This one is very helpful here: https://dplyr.tidyverse.org/articles/programming.html

Worth reading, it's great :)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • to put my question in perspective, i am not able to accept the expression as a input and perform the calcuation in shinyR. – Sooraj Bhagya Sep 03 '18 at 14:37
  • library(shiny) library(plyr) library(dplyr) library(DT) library(data.table) ui <- pageWithSidebar( headerPanel = headerPanel('data'), sidebarPanel = sidebarPanel(# fileInput( # 'mtcars', h4('Uplaodmtcardata in csv format') # ), uiOutput('formula')), mainPanel(dataTableOutput("data")) ) – Sooraj Bhagya Sep 03 '18 at 14:38
  • server <- function(input, output, session) { mtcarsFile <- reactive({ input$mtcars }) xxmtcars <- reactive({ as.data.table(mtcars) }) output$formula <- renderUI({ textInput('formula', h5('formula')) }) formulaPars <- reactive({ !!(input$formula) }) – Sooraj Bhagya Sep 03 '18 at 14:39
  • newCol = reactive({ quo(formulaPars()) }) output$data <- renderDataTable({ as.data.table(mutate(xxmtcars(), cyl + (!!newCol()))) }) } runApp(list(ui = ui, server = server)) – Sooraj Bhagya Sep 03 '18 at 14:39
  • I think this might be worth a new question - as your orginal question has (hopefully) been answered sufficiently with my answer – tjebo Sep 03 '18 at 14:39
  • If you agree, accept this as an answer (checkmark next to the answer). And post a new question , referring to this question here. And do no forget use the [tag:shiny] tag – tjebo Sep 03 '18 at 14:40
  • ShinyR code is upload.I dont know how to share the code when i write a comment. Hence code has been split and sent.Sorry for the inconvenience. – Sooraj Bhagya Sep 03 '18 at 14:40
  • @user10270838 see my comments above – tjebo Sep 03 '18 at 14:41
  • @user10270838 you should not post to much code inside a comment anyways. For code snippets use backticks: ` – tjebo Sep 03 '18 at 14:42
0
library(dplyr)

newCol <- eval(parse(text='gear+wt'),list2env(mtcars))

mutate(mtcars, newCol = newCol)

Or, use base R instead of mutate:

mtcars$newCol <- eval(parse(text='gear+wt'),list2env(mtcars))
mtcars
Dan Houghton
  • 121
  • 2
  • 4