[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  Vectorize: Vectorize a Scalar Function

##  Description

` Vectorize ` creates a function wrapper that vectorizes the action of its
argument ` FUN ` .

##  Usage

    
    
    Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE,
              USE.NAMES = TRUE)

##  Arguments

FUN

function to apply, found via ` [ match.fun
](/link/match.fun?package=base&version=3.6.2) ` .

vectorize.args

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
](/link/sapply?package=base&version=3.6.2) ` .

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.

##  Value

A function with the same arguments as ` FUN ` , wrapping a call to ` [ mapply
](/link/mapply?package=base&version=3.6.2) ` .

##  Details

The arguments named in the ` vectorize.args ` argument to ` Vectorize ` are
the arguments passed in the ` ... ` list to ` [ mapply
](/link/mapply?package=base&version=3.6.2) ` . 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 ](/link/formals?package=base&version=3.6.2) ` .

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.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FVectorize&utm_source=r-docs&utm_medium=docs&utm_term=Vectorize&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    # 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)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2FVectorize&utm_source=r-docs&utm_medium=docs&utm_term=Vectorize&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)