Create a new column by evaluating expression in another

In the following example, the variable expr

contains an expression as a string. However, the last command mutate

does not evaluate this expression correctly. Could you please help me understand why?

symbols <- expand.grid(data.frame(matrix(rep(c("+","-",""),8),ncol=8)))

df <- symbols %>%
  transmute(expr=paste0(1,X1,2,X2,3,X3,4,X4,5,X5,6,X6,7,X7,8,X8,9)) %>%
  mutate(eval=eval(parse(text=expr)))

      

+3


source to share


1 answer


Try rowwise()



df <- symbols %>%
         transmute(expr=paste0(1,X1,2,X2,3,X3,4,X4,5,X5,6,X6,7,X7,8,X8,9)) %>%
         rowwise() %>%
         mutate(v1=eval(parse(text=expr)))

      

+2


source







All Articles