In R, evaluate expressions within vector of strings

左心房为你撑大大i 提交于 2019-11-30 09:54:15

Just apply the whole function.

sapply(foo, function(x) eval(parse(text=x)))

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!