cbind 2 dataframes with different number of rows

I think you should instead use merge: merge(df1, df2, by=”year”, all = T) For your data: df1 = data.frame(matrix(0, 7, 4)) names(df1) = c(“year”, “avg”, “hr”, “sal”) df1$year = 2010:2016 df1$avg = c(.3, .29, .275, .280, .295, .33, .315) df1$hr = c(31, 30, 14, 24, 18, 26, 40) df1$sal = c(2000, 4000, 600, 800, 1000, … Read more

cbind a dataframe with an empty dataframe – cbind.fill?

Here’s a cbind fill: cbind.fill <- function(…){ nm <- list(…) nm <- lapply(nm, as.matrix) n <- max(sapply(nm, nrow)) do.call(cbind, lapply(nm, function (x) rbind(x, matrix(, n-nrow(x), ncol(x))))) } Let’s try it: x<-matrix(1:10,5,2) y<-matrix(1:16, 4,4) z<-matrix(1:12, 2,6) cbind.fill(x,y) cbind.fill(x,y,z) cbind.fill(mtcars, mtcars[1:10,]) I think I stole this from somewhere. EDIT STOLE FROM HERE: LINK