R: Hwo to cbind vectors of different length?

by Toby   Last Updated September 11, 2019 09:19 AM - source

I need to combine some named numeric vectors in R into a data frame. I tried cbind.na as suggestet in another question, but it would not take names into account. Example:

v1 <- c(1,5,6,7)
names(v1) <- c("milk", "flour", "eggs", "sugar")
v2 <- c(2,3)
names(v2) <- c("fish", "chips")
v3 <- c(5,7,4)
names(v3) <- c("chips", "milk", "sugar")

The data frame should look like this

       v1    v2     v3
milk   1     NA     7
flour  5     NA     NA
eggs   6     NA     NA
sugar  7     NA     4
fish   NA    2      NA
chips  NA    3      5

I can't figure out how to solve this in R.

Tags : r

Related Questions