如何将字符串解析为实数方程 [英] How to parse a string into real equation
本文介绍了如何将字符串解析为实数方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将字符串转换为真实方程式.例如:我当前的字符串是
I am trying to convert a string into a real equation. For example: my current string is
> a<-"y = 31.563 + 5.923*x1 + 5.061*x2 - 0.662*x3 - 0.381*x4"
> class(a)
[1] "character"
现在,我有一个新的数据集:
Right now, I have a new data set:
x1=3
x2=3
x3=4
x4=7
df_new = data.frame(x1,x2,x3,x4)
我想根据等式获得 y
的值.因此,我需要将该字符串转换为实方程式.有人知道怎么做吗?
I would like to get the value of y
based on the equation. So I need to convert that string into real equation. Does anybody know how to do that?
推荐答案
这可能不是不是的最佳方法,而是 eval(parse(...))代码>将起作用:
This is probably not the best way to do this, but eval(parse(...))
will work:
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)
我不太确定如何从 eval()
返回的值,所以我摆脱了 y =
部分...
I wasn't quite sure how to get the value returned from eval()
so I got rid of the y=
part ...
这篇关于如何将字符串解析为实数方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文