I wish to evaluate a vector of strings containing arithmetic expressions -- \"1+2\", \"5*6\", etc.
I know that I can parse a single string into an expression and t
Just to show that you can also do this with a for loop:
result <- numeric(length(foo))
foo <- parse(text=foo)
for(i in seq_along(foo))
result[i] <- eval(foo[[i]])
I'm not a fan of using the *apply
functions for their own sake, but in this case, sapply
really does lead to simpler, clearer code.
Just apply the whole function.
sapply(foo, function(x) eval(parse(text=x)))