Jatt
Jatt

Reputation: 785

Generating sequence which each number is n times its predecessor

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

Upvotes: 1

Views: 88

Answers (2)

Jan
Jan

Reputation: 43189

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

Upvotes: 1

conv3d
conv3d

Reputation: 2896

As Max said in a comment:

0.001*3^(0:10)

A decent code golf solution.

Upvotes: 1

Related Questions