I'd like to row-wise evaluate expressions stored as strings in a data.frame. Is there a way to correct the code below so that y is created by the string evaluation using the parameters a and b on the same row?
Thanks in advance
PS: note that this reprex is somewhat sanitize. In my actual workflow, the content of the 'x' column is unknown a priori and string evaluations may lead to errors or objects that are not numeric, so some error trapping would be needed.
require(dplyr) df <- data.frame( a = 1:5, b = 2:6, x = c(NA, 'sin(a)', NA, 'log(b)', NA) ) %>% mutate( y = eval(parse(text = x)), y_expected = c(NA, sin(2), NA, log(5), NA) )