Created
November 5, 2013 04:24
-
-
Save mrdwab/7313857 to your computer and use it in GitHub Desktop.
`interleave` from "gdata" with a `list` as an input.
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
Interleave <- function(myList, append.source = TRUE, sep = ": ", drop = FALSE) { | |
sources <- myList | |
sources[sapply(sources, is.null)] <- NULL | |
sources <- lapply(sources, function(x) if (is.matrix(x) || | |
is.data.frame(x)) | |
x | |
else t(x)) | |
nrows <- sapply(sources, nrow) | |
mrows <- max(nrows) | |
if (any(nrows != mrows & nrows != 1)) | |
stop("Arguments have differening numbers of rows.") | |
sources <- lapply(sources, function(x) if (nrow(x) == 1) | |
x[rep(1, mrows), , drop = drop] | |
else x) | |
tmp <- do.call("rbind", sources) | |
nsources <- length(sources) | |
indexes <- outer((0:(nsources - 1)) * mrows, 1:mrows, "+") | |
retval <- tmp[indexes, , drop = drop] | |
if (append.source && !is.null(names(sources))) | |
if (!is.null(row.names(tmp))) | |
row.names(retval) <- paste(format(row.names(retval)), | |
format(names(sources)), sep = sep) | |
else row.names(retval) <- rep(names(sources), mrows) | |
retval | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment