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.
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.
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)
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