combine vectors per row in R by filling empty spaces

4

I have two vectors:

a <- c(1,2,3,4)
b <- c(1,2)

I want to create an array by combining the vectors per line as follows:

1 2 3 4
1 2 0 0

That is, join two vectors per line and fill in empty spaces.

    
asked by anonymous 11.08.2017 / 21:14

2 answers

3

There is the smartbind function of the gtools package that does what you need

library(gtools)
smartbind(c(1,2,3,4), c(1,2), fill = 0)
    
11.08.2017 / 21:49
1

You can increase the vector b to be the same size as the a vector including zeros:

rbind(a, b = c(b, rep(0, length(a) - length(b))))
  [,1] [,2] [,3] [,4]
a    1    2    3    4
b    1    2    0    0
    
12.08.2017 / 07:16