如何将字符串解析为真正的方程

cut*_*nny 2 string equation r

我正在尝试将字符串转换为真正的方程。例如:我当前的字符串是

> a<-"y = 31.563 + 5.923*x1 + 5.061*x2 - 0.662*x3 - 0.381*x4"
> class(a)
[1] "character"
Run Code Online (Sandbox Code Playgroud)

现在,我有一个新的数据集:

x1=3 
x2=3
x3=4
x4=7
df_new = data.frame(x1,x2,x3,x4)
Run Code Online (Sandbox Code Playgroud)

我想y根据等式得到 的值。所以我需要将该字符串转换为真正的方程。有人知道怎么做吗?

Ben*_*ker 6

这可能不是最好的方法,但eval(parse(...))会起作用:

a <- "y = 31.563 + 5.923*x1 + 5.061*x2 - 0.662*x3 - 0.381*x4"
df_new <- data.frame(x1=3,x2=3,x3=4,x4=7)
eval(parse(text=gsub("y =","",a)),df_new)
Run Code Online (Sandbox Code Playgroud)

我不太确定如何获得返回的值,eval()所以我摆脱了这个y=部分......