Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial checkin (v0.3)
  • Loading branch information
puetz committed Aug 11, 2017
0 parents commit b9ae1d8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions vcomb.R
@@ -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))
}

0 comments on commit b9ae1d8

Please sign in to comment.