将字符更改为可由curve()函数使用的方程。

huangapple go评论53阅读模式
英文:

R change character to equation that can be used by curve() function

问题

我有以下字符字符串
"x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284"
我想将它转换成可以传递给曲线函数的形式。
curve(x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284, add = TRUE)
有人知道这个问题的解决方法吗?
提前致谢,
Kuba

英文:

I have the following character string

"x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284"

I would like to convert it to a form that can be passed to the curve function.

curve(x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284, add = TRUE)

Does anyone know the solution to this problem?

Thanks in advance,
Kuba

答案1

得分: 4

I suppose in this situation you could use parse:

str <- "x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284"
func <- as.function(c(alist(x =), parse(text = str)[[1]]))
curve(func)

将字符更改为可由curve()函数使用的方程。

英文:

I suppose in this situation you could use parse:

str <- "x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284"
func <- as.function(c(alist(x =), parse(text = str)[[1]]))
curve(func)

将字符更改为可由curve()函数使用的方程。

答案2

得分: 3

不建议使用,但你仍然可以尝试 `eval` + `str2lang`
英文:

Not recommended but you still can try eval + str2lang

s <- "x*3.55771902829131+x*-1.26223375202399+x*-0.486886610871284"
eval(str2lang(sprintf("curve(%s)", s)))

huangapple
  • 本文由 发表于 2023年5月22日 21:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定