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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With