3
我有一個公式作爲字符矢量,並且想在curve
中使用它。如何使用公式作爲曲線中的字符矢量()
curve(-8*cos(2*x), 0, 10) #works as expected
formula <- "-8*cos(2*x)"
# nothing works
curve(formula, 0, 10)
# Error in formula.default(x) : invalid formula
curve(eval(formula), 0, 10)
# Error in curve(eval(formula), 0, 10) :
# 'expr' must be a function, or a call or an expression containing 'x'
curve(as.expression(formula), 0, 10)
# Error in curve(as.expression(formula), 0, 10) :
# 'expr' must be a function, or a call or an expression containing 'x'
curve(eval(as.expression(formula)), 0, 10)
# Error in curve(eval(as.expression(formula)), 0, 10) :
# 'expr' must be a function, or a call or an expression containing 'x'
curve(parse(text = formula), 0, 10)
# Error in curve(parse(text = formula), 0, 10) :
# 'expr' must be a function, or a call or an expression containing 'x'
curve(eval(parse(text = formula)), 0, 10)
# Error in curve(eval(parse(text = formula)), 0, 10) :
# 'expr' must be a function, or a call or an expression containing 'x'
我的問題
我怎樣才能使這項工作?
燁。這或者'do.call(曲線,列表(解析(文本=公式),0,10))''。 –
爲什麼你需要函數末尾的'(x)'?我從來沒有猜到過...... – vonjd