From ebb9617950a6a968d50f29edb92ca931d3fd9faa Mon Sep 17 00:00:00 2001 From: mattbk Date: Tue, 22 Oct 2019 12:54:05 -0500 Subject: [PATCH] Create cbind.all.R --- cbind.all.R | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 cbind.all.R diff --git a/cbind.all.R b/cbind.all.R new file mode 100644 index 0000000..7dec73d --- /dev/null +++ b/cbind.all.R @@ -0,0 +1,34 @@ +# C.3.1 cbind.all +# C.3.1.1 Summary +# Written by Dorai-Raj (2005). Does the same thing as cbind(), but will join lists +of unequal lengths. Function is used in euc.group(). +# Dorai-Raj, S. (2005). [R] R: cbind from Sundar Dorai-Raj on 2005-08-08 (2005- +# August.txt). http:// nzi.psych.upenn.edu/R/Rhelp02a/archive/59302.html. Accessed +# 10 November 2008. +# C.3.1.2 Requirements +# None. +# C.3.1.3 User Input +# A list of column vectors (which can be di erent lengths) is required as arguments. +# C.3.1.4 Output +# Output is a data matrix containing the columns entered, arranged side by side. + +cbind.all <- function(..., fill.with = NA) { +args <- list(...) +len <- sapply(args, NROW) +if(diff(rng <- range(len)) > 0) { +maxlen <- rng[2] +pad <- function(x, n) c(x, rep(fill.with, n)) +for(j in seq(along = args)) { +if(maxlen == len[j]) next +if(is.data.frame(args[[j]])) { +args[[j]] <- lapply(args[[j]], pad, maxlen - len[j]) +args[[j]] <- as.data.frame(args[[j]]) +} else if(is.matrix(args[[j]])) { +args[[j]] <- apply(args[[j]], 2, pad, maxlen - len[j]) +} else if(is.vector(args[[j]])) { +args[[j]] <- pad(args[[j]], maxlen - len[j]) +} else { +stop("... must only contain data.frames or arrays.") +} +} +}