Last active
April 12, 2020 22:51
-
-
Save mrdwab/6789277 to your computer and use it in GitHub Desktop.
Cbind for unequal length vectors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
padNA <- function (mydata, rowsneeded, first = TRUE) | |
{ | |
temp1 = colnames(mydata) | |
rowsneeded = rowsneeded - nrow(mydata) | |
temp2 = setNames( | |
data.frame(matrix(rep(NA, length(temp1) * rowsneeded), | |
ncol = length(temp1))), temp1) | |
if (isTRUE(first)) rbind(mydata, temp2) | |
else rbind(temp2, mydata) | |
} | |
dotnames <- function(...) { | |
vnames <- as.list(substitute(list(...)))[-1L] | |
vnames <- unlist(lapply(vnames,deparse), FALSE, FALSE) | |
vnames | |
} | |
Cbind <- function(..., first = TRUE) { | |
Names <- dotnames(...) | |
datalist <- setNames(list(...), Names) | |
nrows <- max(sapply(datalist, function(x) | |
ifelse(is.null(dim(x)), length(x), nrow(x)))) | |
datalist <- lapply(seq_along(datalist), function(x) { | |
z <- datalist[[x]] | |
if (is.null(dim(z))) { | |
z <- setNames(data.frame(z), Names[x]) | |
} else { | |
if (is.null(colnames(z))) { | |
colnames(z) <- paste(Names[x], sequence(ncol(z)), sep = "_") | |
} else { | |
colnames(z) <- paste(Names[x], colnames(z), sep = "_") | |
} | |
} | |
padNA(z, rowsneeded = nrows, first = first) | |
}) | |
do.call(cbind, datalist) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a small example: