vectorize | R Documentation |
Robust alternative to Vectorize
function that accepts any function with two
or more arguments. Returns a function that will work an arbitrary number of vectors, lists or
data frames, though output may be unpredicatable in unusual applications. The
results are also intended to be more intuitive than Vectorize.
Originally written for the row.r package by Craig Varrichio. Included here because the rowr
package was discontinued. I use these functions in my packages
vectorize(fun, type = NULL)
fun |
a two or more argument function |
type |
like MARGIN in apply, except that c(1,2) is represented as a 3 instead. By default, will Reduce single dimensional data handle everything else row-wise. |
vectorize(`+`)(c(1,2,3))
vectorize(sum)(c(1,2,3),c(1,2,3))
# Compare these results to Vectorize, which does not vectorize sum at all.
Vectorize(sum)(c(1,2,3),c(1,2,3))
# Across data frame columns.
df<-data.frame(a=c(1,2,3),b=c(1,2,3))
vectorize(sum)(df$a,df$b)
# Once again, Vectorize gives a different result
Vectorize(sum)(df$a,df$b)
# Any combination of vectors, lists, matrices, or data frames an be used.
vectorize(`+`)(c(1,2,3),list(1,2,3),cbind(c(1,2,3)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.