有没有办法舍入我在函数poly.calc的系数中得到的小数?

时间:2019-05-26 18:26:06

标签: r rounding

我正在使用poly.calc()函数对某些点进行插值,问题是我需要为每个系数打印最多两个小数的多项式

我知道R中有一个如round(...)的函数,它可以执行此操作,但是可以使用数字,我不知道它是否可以与我从poly.calc()获得的表达式一起使用。 / p>

一个例子:

library(PolynomF)

x=c(1:4)
y=c(15,12,14,17)

pol = poly.calc(x,y)
pol

这是结果:

  

27-17.83333 * x + 6.5 * x ^ 2-0.6666667 * x ^ 3

我需要这样的东西:

  

27-17.83 * x + 6.5 * x ^ 2-0.66 * x ^ 3

2 个答案:

答案 0 :(得分:3)

我们可以利用Office.onReady

round

数据

library(PolynomF)
pol <- round(pol, 2)
pol
#27 - 17.83*x + 6.5*x^2 - 0.67*x^3 

答案 1 :(得分:0)

您将尝试使用以下示例中所示的取整功能并根据需要进行编辑:

# --- calculate parameters lm -----------------------------------------
model <- lm(Pegel$D00 ~ Pegel$dss)
# summary(model)
a = round(as.vector(coef(model)[1]), 2)
b = round(as.vector(coef(model)[2]*365), 2) # scaled for year
r2 = round(summary(model)$r.squared, 2)
相关问题