Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
initial checkin (v0.3)
- Loading branch information
0 parents
commit b9ae1d8
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
##' combine all possible pairs of vectors \code{v1} and \code{v2} | ||
##' using function \code{myfun} | ||
##' | ||
##' In essence, this is a generalized version of \code{\link[base]{outer}} | ||
##' allowing non-scalar return values for \code{myfun}. | ||
##' | ||
##' Possible extensions: | ||
##' \enumerate{ | ||
##' \item reformat the output to be matrix-like (see outer) | ||
##' \item use a list of index pairs with \code{lapply} to allow | ||
##' irregular return values | ||
##' \item shuffle the order of elements (incompatible with (1)) | ||
##' } | ||
##' @title vector combine | ||
##' @param v1 first vector | ||
##' @param v2 second vector | ||
##' @param myfun function to combine the element pairs from the two vectors | ||
##' @param ... not used | ||
##' @return vector (or matrix, array) of combinations | ||
##' @author Benno Puetz \email{puetz@@mpipsykl.mpg.de} | ||
##' @examples | ||
##' a <- 1:3 | ||
##' b <- 5:9 | ||
##' vcomb(a, b) | ||
vcomb <- function(v1, v2, myfun = c, ...){ | ||
mat <- matrix(as.numeric(unlist(strsplit(outer(seq_along(v1), | ||
seq_along(v2), | ||
FUN = function(a, b){ | ||
paste(a, b, | ||
sep = '.') | ||
}), | ||
'.', | ||
fixed = TRUE) | ||
)), | ||
ncol = 2, | ||
byrow = TRUE) | ||
combine.vectors <- function(rv) { | ||
retval <- myfun(v1[rv[1]], v2[rv[2]]) | ||
return(retval) | ||
} | ||
return(apply(mat, 1, combine.vectors)) | ||
} |