0

I would like two vectors with unequal lengths to be combined but rbind() returns like

a <- 1:5
b <- 1:10
rbind(a,b)

#   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# a    1    2    3    4    5    1    2    3    4     5
# b    1    2    3    4    5    6    7    8    9    10

but I would like my data to be

rbind(a,b)

#   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# a    1    2    3    4    5    0    0    0    0     0
# b    1    2    3    4    5    6    7    8    9    10

and make a layered histogram with one layer

zx8754
  • 52,746
  • 12
  • 114
  • 209
james
  • 11
  • 1
  • 8

1 Answers1

0

You could adjust the vector lengths to the maximum lengths, which will give you NAs, and replace those NAs by 0s:

lst <- list(a, b)
res <- do.call(rbind, lapply(lst, "length<-", max(lengths(lst))))
res[is.na(res)] <- 0
res
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    1    2    3    4    5    0    0    0    0     0
# [2,]    1    2    3    4    5    6    7    8    9    10
lukeA
  • 53,097
  • 5
  • 97
  • 100