How do you bind uneven columns?

I am trying to make a table containing the binded columns of two other tables. These two tables are of an uneven length. I would like to be able to bind them regardless and "fill" the length of the shorter column(s) with NAs, so these are effectively full joined on the row number.

When attempting to bind , I am receiving an error. For example:

a <- c("a", "b", "c")
b <- c(1, 2)

bind_cols(a, b)

"Error: Can't recycle `..1` (size 3) to match `..2` (size 2)."

My desired output is something like

  a  b
1 a  1
2 b  2
3 c NA

>Solution :

data.frame(lapply(m<-list(a = a, b = b), `length<-`, max(lengths(m))))
  a  b
1 a  1
2 b  2
3 c NA

Leave a Reply