my question seems to be a mixture of the following two threads:
Combine two data frames by rows (rbind) when they have different sets of columns
rbind data frames based on a common pattern in data frame name
I want to combine (by adding rows) the content of several vectors with different lengths based on the pattern of the vector name. Example data set:
million_cities_USA <- c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas")
million_cities_UK <- c("London", "Birmingham")
million_cities_Canada <- c("Toronto", "Montreal", "Calgary", "Ottawa", "Edmonton")
When I combine the vectors with rbind (see second answer in the second link), R starts recycling the shorter vectors:
as.data.frame(do.call(rbind, mget(ls(pattern="million_cities_*"))))
V1 V2 V3 V4 V5 V6 V7 V8 V9
million_cities_Canada Toronto Montreal Calgary Ottawa Edmonton Toronto Montreal Calgary Ottawa
million_cities_UK London Birmingham London Birmingham London Birmingham London Birmingham London
million_cities_USA New York Los Angeles Chicago Houston Phoenix Philadelphia San Antonio San Diego Dallas
Regarding the first link, I suppose that this can be avoided with dplyr's bind_rows. However, I couldn't combine the vectors at all with bind_rows. The error message indicated that bind_rows needs vectors with identical lengths to work:
library(dplyr)
as.data.frame(mget(ls(pattern="million_cities_*")) %>%
bind_rows())
Error: Argument 2 must be length 5, not 2
How can I combine all vectors with the same name pattern by row and leave the missing columns of the shorter vectors empty instead of inserting the vector elements again?