如何创建正序和负序

How to create a postive and negative sequence

我目前正在研究 r 以使自己达到标准。 尝试创建一个

的序列

(-10,10,100,100,1000,1000)

我的第一个问题是如何创建一个在正负之间交替的序列

其次,我如何创建一个序列,每 x 个数字将自身乘以 10。

您可以使用 repcumprod 创建一个自乘 10 的序列,然后乘以 c(-1,1)正反交替:

rep(cumprod(rep(10, 3)), each=2) * c(-1, 1)
#[1]   -10    10  -100   100 -1000  1000

另一种思路可以,

rep(10^seq(3), each = 2) * c(-1, 1)
#[1]   -10    10  -100   100 -1000  1000

只是添加了一个基准。事实证明,cumprod 很快

microbenchmark(sotos = rep(10 ^ seq(30), each = 2) * c(-1, 1), 
               GKi = rep(cumprod(rep(10, 30)), each = 2) * c(-1, 1))
Unit: microseconds
  expr   min    lq    mean median    uq    max neval
 sotos 6.911 7.212 7.83658  7.512 7.513 30.348   100
   GKi 1.502 1.803 2.03470  1.804 2.104 11.118   100