英文:
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)
英文:
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)
答案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)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论