1

How could I generate a sequence where each number is n * previous_number.

For example, in a sequence:

0.001 0.003 0.009 0.027

Each number is 3 times its predecessor. I was trying to use seq like:

seq(from = 0.001, by = 3, length.out = 10)

But it prints the output like:

0.001  3.001  6.001  9.001 12.001 15.001 18.001 21.001 24.001 27.001
M--
  • 25,431
  • 8
  • 61
  • 93
Jatt
  • 665
  • 2
  • 8
  • 20

2 Answers2

1

As Max said in a comment:

0.001*3^(0:10)

A decent code golf solution.

conv3d
  • 2,668
  • 6
  • 25
  • 45
  • 2
    `0.001*c(1,cumprod(rep(3,10)))` isn't quite as compact (probably a little faster, if you care about microseconds) – Ben Bolker Apr 03 '18 at 19:52
1

You could write a little function:

seq_func <- function(x, m, len = 10) {
  return(x*m^(0:len))
}

seq_func(0.001, 3)

Which would yield

[1]  0.001  0.003  0.009  0.027  0.081  0.243  0.729  2.187  6.561 19.683
Jan
  • 42,290
  • 8
  • 54
  • 79