如何以零作为中点创建一个从min到最大的数字序列?

发布于 2025-02-11 09:33:23 字数 284 浏览 1 评论 0原文

如果我有以下变量。

set.seed(20)
variable <- sample(-74:124.2, size = 38)

如何使用变量的最小值和零作为中点(第3个值)创建5个数字的序列?

seq()采用启动和结束值,但是有没有办法通过指定中间值来做到这一点?

seq(min(variable), max(variable), length.out = 5)

If I had the following variable.

set.seed(20)
variable <- sample(-74:124.2, size = 38)

How can I create a sequence of 5 numbers using the min and max values of variable and zero as the midpoint (3rd value)?

seq() takes the start and end values, but is there a way to do this by specifying a middle value?

seq(min(variable), max(variable), length.out = 5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花开浅夏 2025-02-18 09:33:23

您可以用指定的值替换生成序列的中间:

seq(min(variable), max(variable), length.out = 5) -> x        
x[which(x == median(x))] <- 0

#-73.00 -25.75   0.00  68.75 116.00

如果length(x)是奇数,则可以使用。

You can replace the median of the generated sequence with a specified value:

seq(min(variable), max(variable), length.out = 5) -> x        
x[which(x == median(x))] <- 0

#-73.00 -25.75   0.00  68.75 116.00

This would work if the length(x) is odd.

故人如初 2025-02-18 09:33:23

您可以组合两个seq

c(seq(min(variable), 0, length.out=3), 
  seq(0, max(variable), length.out=3)[-1])
#[1] -73.0 -36.5   0.0  58.0 116.0

这仅在min(variable)&lt; = 0max(variable)&gt; = 0时,才能按预期工作。

You can combine two seq.

c(seq(min(variable), 0, length.out=3), 
  seq(0, max(variable), length.out=3)[-1])
#[1] -73.0 -36.5   0.0  58.0 116.0

This will only work as expected when min(variable) <= 0 and max(variable) >= 0.

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