Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a postive and negative sequence [closed]

Tags:

r

sequence

I'm currently working on r to bring myself upto scratch. Trying to create a sequence which is

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

My first question is how do I create a sequence that alternates between positive and negative

Secondly how do I create a sequence that multiplies itself by 10 every x amount of numbers.

like image 564
M.Ustun Avatar asked Dec 06 '22 08:12

M.Ustun


1 Answers

You can use rep and cumprod creating a sequence that multiplies itself by 10 and multiply with c(-1,1) to alternates between positive and negative:

rep(cumprod(rep(10, 3)), each=2) * c(-1, 1)
#[1]   -10    10  -100   100 -1000  1000
like image 136
GKi Avatar answered Dec 28 '22 05:12

GKi