a character vector of arguments which should be
vectorized. Defaults to all arguments of FUN.
SIMPLIFY
logical or character string; attempt to reduce the
result to a vector, matrix or higher dimensional array; see
the simplify argument of sapply.
USE.NAMES
logical; use names if the first ... argument has
names, or if it is a character vector, use that character vector as
the names.
Details
The arguments named in the vectorize.args argument to
Vectorize are the arguments passed in the ... list to
mapply. Only those that are actually passed will be
vectorized; default values will not. See the examples.
Vectorize cannot be used with primitive functions as they do
not have a value for formals.
It also cannot be used with functions that have arguments named
FUN, vectorize.args, SIMPLIFY or
USE.NAMES, as they will interfere with the Vectorize
arguments. See the combn example below for a workaround.
Value
A function with the same arguments as FUN, wrapping a call to
mapply.
Examples
# We use rep.int as rep is primitive
vrep <- Vectorize(rep.int)
vrep(1:4, 4:1)
vrep(times = 1:4, x = 4:1)
vrep <- Vectorize(rep.int, "times")
vrep(times = 1:4, x = 42)
f <- function(x = 1:3, y) c(x, y)
vf <- Vectorize(f, SIMPLIFY = FALSE)
f(1:3, 1:3)
vf(1:3, 1:3)
vf(y = 1:3) # Only vectorizes y, not x
# Nonlinear regression contour plot, based on nls() example
require(graphics)
SS <- function(Vm, K, resp, conc) {
pred <- (Vm * conc)/(K + conc)
sum((resp - pred)^2 / pred)
}
vSS <- Vectorize(SS, c("Vm", "K"))
Treated <- subset(Puromycin, state == "treated")
Vm <- seq(140, 310, length.out = 50)
K <- seq(0, 0.15, length.out = 40)
SSvals <- outer(Vm, K, vSS, Treated$rate, Treated$conc)
contour(Vm, K, SSvals, levels = (1:10)^2, xlab = "Vm", ylab = "K")
# combn() has an argument named FUN
combnV <- Vectorize(function(x, m, FUNV = NULL) combn(x, m, FUN = FUNV),
vectorize.args = c("x", "m"))
combnV(4, 1:4)
combnV(4, 1:4, sum)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(base)
> png(filename="/home/ddbj/snapshot/RGM3/R_rel/result/base/Vectorize.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Vectorize
> ### Title: Vectorize a Scalar Function
> ### Aliases: Vectorize
> ### Keywords: manip utilities
>
> ### ** Examples
>
> # We use rep.int as rep is primitive
> vrep <- Vectorize(rep.int)
> vrep(1:4, 4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
> vrep(times = 1:4, x = 4:1)
[[1]]
[1] 4
[[2]]
[1] 3 3
[[3]]
[1] 2 2 2
[[4]]
[1] 1 1 1 1
>
> vrep <- Vectorize(rep.int, "times")
> vrep(times = 1:4, x = 42)
[[1]]
[1] 42
[[2]]
[1] 42 42
[[3]]
[1] 42 42 42
[[4]]
[1] 42 42 42 42
>
> f <- function(x = 1:3, y) c(x, y)
> vf <- Vectorize(f, SIMPLIFY = FALSE)
> f(1:3, 1:3)
[1] 1 2 3 1 2 3
> vf(1:3, 1:3)
[[1]]
[1] 1 1
[[2]]
[1] 2 2
[[3]]
[1] 3 3
> vf(y = 1:3) # Only vectorizes y, not x
[[1]]
[1] 1 2 3 1
[[2]]
[1] 1 2 3 2
[[3]]
[1] 1 2 3 3
>
> # Nonlinear regression contour plot, based on nls() example
> require(graphics)
> SS <- function(Vm, K, resp, conc) {
+ pred <- (Vm * conc)/(K + conc)
+ sum((resp - pred)^2 / pred)
+ }
> vSS <- Vectorize(SS, c("Vm", "K"))
> Treated <- subset(Puromycin, state == "treated")
>
> Vm <- seq(140, 310, length.out = 50)
> K <- seq(0, 0.15, length.out = 40)
> SSvals <- outer(Vm, K, vSS, Treated$rate, Treated$conc)
> contour(Vm, K, SSvals, levels = (1:10)^2, xlab = "Vm", ylab = "K")
>
> # combn() has an argument named FUN
> combnV <- Vectorize(function(x, m, FUNV = NULL) combn(x, m, FUN = FUNV),
+ vectorize.args = c("x", "m"))
> combnV(4, 1:4)
[[1]]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 2 2 3
[2,] 2 3 4 3 4 4
[[3]]
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
[[4]]
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
> combnV(4, 1:4, sum)
[[1]]
[1] 1 2 3 4
[[2]]
[1] 3 4 5 5 6 7
[[3]]
[1] 6 7 8 9
[[4]]
[1] 10
>
>
>
>
>
> dev.off()
null device
1
>