I am taking my first steps in R. Have a series of exercises, but one is especially dificult for me. I need to create an series like this:
a1, b10, c100, d1000, ..., j1000000000
Combining following letters with numbers is not an issue, but how generate series of numbers, where each of them is previous one multiplied by 10? I have gut feeling that it is not hard at all but I can't figure it out.
CodePudding user response:
we can use ^
.
We may have to use options(scipen=999)
to avoid incorporating scientific notation into the final character vector, if we have large numbers. some floating point issues may arise if the numbers get too high.
options(scipen=999)
paste0(letters, 10^(0:25))
[1] "a1" "b10"
[3] "c100" "d1000"
[5] "e10000" "f100000"
[7] "g1000000" "h10000000"
[9] "i100000000" "j1000000000"
[11] "k10000000000" "l100000000000"
[13] "m1000000000000" "n10000000000000"
[15] "o100000000000000" "p1000000000000000"
[17] "q10000000000000000" "r100000000000000000"
[19] "s1000000000000000000" "t10000000000000000000"
[21] "u100000000000000000000" "v1000000000000000000000"
[23] "w10000000000000000000000" "x100000000000000008388608"
[25] "y 999999999999999983222784" "z10000000000000000905969664"
CodePudding user response:
Use
10^(0:n)
For example
> 10^(0:3)
[1] 1 10 100 1000
Or even use a better way
x<-10^(0:3)
names(x)<-paste0("a_",0:3)
> x
a_0 a_1 a_2 a_3
1 10 100 1000