是什么导致我在R中的分段函数中引起此错误?
我正在尝试通过以下参数在R中发挥分段函数:
##### Rules ####
# If x from 2 to 6, x2
# If x < 2, x-5
# If x > 6, x^9
因此,我已经制作了此功能来尝试适合这些参数:
piecewise <- function(x){
return(ifelse(x < 2, x - 5,
ifelse(x > 6, x^9, x^2)))
}
我创建了一个向量来测试并将其适合该功能:
x <- c(1,20,5,2,30)
piecewise(x)
但是,它给了我以下奇数输出:
[1] -4.0000e+00 5.1200e+11 2.5000e+01 4.0000e+00 1.9683e+13
是什么原因导致了此问题,我该如何解决?
I'm trying to make a piecewise function in R with the following arguments:
##### Rules ####
# If x from 2 to 6, x2
# If x < 2, x-5
# If x > 6, x^9
Thus, I have made this function to try to fit these arguments:
piecewise <- function(x){
return(ifelse(x < 2, x - 5,
ifelse(x > 6, x^9, x^2)))
}
I created a vector to test this out and fit it into the function:
x <- c(1,20,5,2,30)
piecewise(x)
However, it gives me the following odd output:
[1] -4.0000e+00 5.1200e+11 2.5000e+01 4.0000e+00 1.9683e+13
What is causing this issue and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过设置
scipen
“科学符号惩罚”选项来更改R的打印选项,以改变科学符号。数字少于scipen
的数字将在没有科学符号的情况下打印:请参阅
?options
有关更多详细信息。You can change R's print options for scientific notation by setting the
scipen
"scientific notation penalty" option. Numbers with fewer digits thanscipen
will be printed without scientific notation:See
?options
for more details.