是什么导致我在R中的分段函数中引起此错误?

发布于 2025-02-10 21:17:48 字数 523 浏览 2 评论 0原文

我正在尝试通过以下参数在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2025-02-17 21:17:48

您可以通过设置scipen“科学符号惩罚”选项来更改R的打印选项,以改变科学符号。数字少于scipen的数字将在没有科学符号的情况下打印:

options(scipen = 35)
x <- c(1,20,5,2,30)
piecewise(x)
# [1]             -4   512000000000             25              4 19683000000000

请参阅?options有关更多详细信息。

You can change R's print options for scientific notation by setting the scipen "scientific notation penalty" option. Numbers with fewer digits than scipen will be printed without scientific notation:

options(scipen = 35)
x <- c(1,20,5,2,30)
piecewise(x)
# [1]             -4   512000000000             25              4 19683000000000

See ?options for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文