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

后端 未结 9 1775
野的像风
野的像风 2020-11-22 11:25

I think I\'m looking for an analog of rbind.fill (in Hadley\'s plyr package) for cbind. I looked, but there is no cbind.fill

9条回答
  •  自闭症患者
    2020-11-22 12:08

    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

提交回复
热议问题