2

I want to write a R program that creates the vector 0.1^3, 0.2^1, 0.1^6, 0.2^4, ..., 0.1^36, 0.2^34.

v=c(seq(3,36,3))
w=c(seq(1,34,3))
x=c(0.1^v)
y=c(0.2^w)
z=c(x,y)

Please help.

2 Answers2

3

rbind to a matrix and convert to vector again:

c(rbind(x, y))

Or more directly:

rep(c(0.1, 0.2), 12)^c(rbind(seq(3,36,3), seq(1,34,3)))
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • 1
    Solution inspired by https://stackoverflow.com/questions/12044616/alternate-interweave-or-interlace-two-vectors, arguably the question here should be marked as a duplicate – s_baldur Jun 22 '21 at 11:02
1

You can use matrix to create the desired vector.

c(matrix(z, 2, byrow=TRUE))
# [1] 1.000000e-03 2.000000e-01 1.000000e-06 1.600000e-03 1.000000e-09
# [6] 1.280000e-05 1.000000e-12 1.024000e-07 1.000000e-15 8.192000e-10
#[11] 1.000000e-18 6.553600e-12 1.000000e-21 5.242880e-14 1.000000e-24
#[16] 4.194304e-16 1.000000e-27 3.355443e-18 1.000000e-30 2.684355e-20
#[21] 1.000000e-33 2.147484e-22 1.000000e-36 1.717987e-24
GKi
  • 37,245
  • 2
  • 26
  • 48