I have a vector "I" which we can take for example to be (0,1,2,3.....) till an unknown length. I want to create a vector such that x^(I-1) so it will be a vector that looks like (0,x,x^2...). However I do not know how to write this code and I tried (x^I-1) alone doesn't work.
-
2R is vectorised in most operations, so you can just do (for example) `2 ^ (0:20)` if x is 2 and you need to go up to 20 – Calum You Feb 18 '20 at 20:48
-
2Sequences can use `seq(from, to, by)` (with other forms) or the less-flexible terse version of `from:to` (e.g., `0:20`). Once you have that vector, as @CalumYou said, you can raise that to a power (`x <- 0:20; x^2`) or raise a scalar (or same-length vector) to it as a power, (`x <- 0:20; 2^x`). – r2evans Feb 18 '20 at 20:58
-
@r2evans Is it true that using the form ``0:100`` is much faster than ``seq(0, 100, 1)`` (on huge data obviously) ? I've been told this and I wanted to know the logic behind this. Is ``:`` like using ``seq.int`` which is a primitive C function while ``seq()`` is a build up R function ? – Gainz Feb 18 '20 at 21:03
-
1What exactly "didn't work" with your attempt. Please show the code you tried. It's easier to help when you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Feb 18 '20 at 21:04
-
2You can see for yourself if you'd like ... if your have `microbenchmark` installed, then `microbenchmark::microbenchmark(a=0:100, b=seq(0,100))`. If not, then `system.time` is not going to give you the precision you want. But ***no***, `seq(0,100)` takes about 35x the time (median) to execute when compared to `0:100`. Granted, those are measures in nanoseconds, so my [blink](https://sciencing.com/fast-blink-eye-5199669.html) is still over 13000x `seq` ... – r2evans Feb 18 '20 at 21:08
-
1BTW: I carried that up to `0:1000000` and it was about 7x faster (median) than `seq`. As always, it depends. – r2evans Feb 18 '20 at 21:12
-
1@r2evans LOL, thanks for clearing this up I appreciate. – Gainz Feb 18 '20 at 21:12
3 Answers
As commented on your question, R is vectorized. This means that you can specify a vector y
of integers, a variable x
of length 1, and then take x
to the power of y
via x^y
:
y <- 0:10
x <- 2
x^y
which returns:
[1] 1 2 4 8 16 32 64 128 256 512 1024

- 7,734
- 4
- 37
- 69
EDIT*** Read r2evan's comments to understand why this is not a recommended solution.
This will take vector z and feed it it into the function to create the resulting vector. Simply adjust your x value (in this case, the 2)
z <- c(1:5)
new_vector <- sapply(z, function(y) 2 ^ y)
The output values in the new vector then would be x^0, x^1, x^2, x^3... etc.

- 1,818
- 7
- 16
-
3While this works, it is not R's idiomatic way of doing things as vectors whenever possible. For instance, if we compare this versus vectorized using `0:100`, the vectorized version takes over 10x as long to execute. There are definitely times when vectorized math cannot be done, this is not one of them. (Not knocking the correctness, just advising on efficiency and R-idiomaticity.) – r2evans Feb 18 '20 at 21:10
You can use seq(l)
to construct a l
-length vector from 1
to l
as powers. Since you are pursuing a start from 0
, so you can use seq(l)-1
.
Then, you choose a value of x
, and generate the desired vector via
x**(seq(l)-1)
If you have x
and l
as variables, you can define your custom sequence generator like below
vgen <- function(x,l) x**(seq(l)-1)
and use it like
> vgen(3,7)
[1] 1 3 9 27 81 243 729

- 96,636
- 9
- 24
- 81