Barnaby
Barnaby

Reputation: 1480

Add the number 0 in front of a vector a number of times to obtain a vector of desired length

I have a vector with 349 data points

x<-(c(1:349))

I would like to add the number 0 in front of the vector a number of times equal to the difference of the vector lenght without 0s and 512. (to obtain a final vector of 512 data points)

Thank you

Bernabe

Upvotes: 1

Views: 179

Answers (2)

agstudy
agstudy

Reputation: 121608

Less idiomatic option :)

y <- vector(mode='numeric',length=512)
y[seq_along(x)] <- rev(x)
rev(y)

Upvotes: 1

NPE
NPE

Reputation: 500883

> c(rep(0, 512-length(x)), x)
  [1]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
 [19]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
...
[145]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
[163]   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
...
[487] 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
[505] 342 343 344 345 346 347 348 349

Upvotes: 3

Related Questions