Reputation: 764
Suppose x=c(5,6,8,9,10)
, I would like to create another variable y
of length 8
. The 3rd
, 5th
and 7th
position should be zero and the rest of the positions are filled with x
- values.
The expected y
is c(5,6,0,8,0,9,0,10)
Any help is appreciated.
Upvotes: 0
Views: 26
Reputation: 388807
We can create an empty vector of length 8, assign values of x
to y
removing index at pos
.
pos <- c(3, 5, 7)
y <- integer(length = 8)
y[-pos] <- x
y
#[1] 5 6 0 8 0 9 0 10
Upvotes: 3