Reputation: 93
How can I create this kind of array in R?
iii <- seq(from = 1, to = 49, by = 2)
this only creates values:
1 3 5 .. 49
The array that I need to create:
1, 0, 3, 0, 5, 0, 7, . . . , 0, 49
Upvotes: 1
Views: 32
Reputation: 83215
Using:
x <- 1:11
x * (x %% 2)
gives:
[1] 1 0 3 0 5 0 7 0 9 0 11
What this does:
x %% 2
creates a vector of one's for the uneven values of x
and zero's for the even values of x
.x
with x %% 2
thus gives a vector with uneven values with zero's in between.Based the suggestion of @lmo, you could also do:
x <- seq(1, 11, 2)
head(rep(x, each = 2) * (1:0), -1)
which will give the same result.
Upvotes: 1