Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
“R vectorize” means two related things. First, R’s built-in vectorized operations: arithmetic and many functions work on a whole vector at once, so you rarely need a loop. Second, the base function Vectorize(), which wraps a function that accepts single values so that it accepts vectors. This guide covers both, the recycling rule that causes the most surprises, and a decision table for choosing between a built-in operation, ifelse(), Vectorize(), mapply() and a loop. Every function signature and rule below was checked against the R manual pages listed under Sources on 2026-10-29.
What vectorization is used for in research
Research data usually arrives as columns of measurements: concentrations, scores, dates, counts. Vectorization lets you apply one calculation to every element of a column in a single expression instead of writing a loop. The R Language Definition describes the basis for this: “R deals with entire vectors of data at a time.” If you are new to the language, what R programming is used for gives the wider context, and for loops in R covers the loop style that vectorization often replaces.
Install or access
Nothing beyond base R is needed. Vectorize(), mapply() and ifelse() are all documented in the base package, which ships with R. R itself is downloaded from the Comprehensive R Archive Network (CRAN) at cran.r-project.org. The manual pages for every function used here are published at stat.ethz.ch/R-manual.
Step-by-step: vectorize a calculation in R
1. Use a built-in vectorized operation
Adding two vectors of equal length gives the element-wise sums, and the same holds for other arithmetic operators. No loop is required.
x = c(2, 4, 6)
y = c(10, 20, 30)
x + y # 12 24 36
x * y # 20 80 180
One detail from the R Language Definition: multiplying two matrices with * does not produce the usual matrix product. It multiplies element by element. Use %*% for the matrix product.
2. Know the recycling rule
When vectors differ in length, the shorter one is recycled to the length of the longer one. If the length of the longer vector is not a multiple of the length of the shorter one, R gives a warning.
c(1, 2, 3, 4) + c(10, 20) # 11 22 13 24 (10, 20 repeated; no warning)
c(1, 2, 3) + c(10, 20) # 11 22 13 (warning: 3 is not a multiple of 2)
c(1, 2, 3) + numeric(0) # numeric(0) (a zero-length operand gives a zero-length result)
3. Use ifelse() for element-wise choices
The usage is ifelse(test, yes, no). The value is a vector of the same length and attributes (including dimensions and class) as test.
n = c(1, 2, 3, 4)
ifelse(n %% 2 == 0, "even", "odd") # "odd" "even" "odd" "even"
The manual cautions that the mode of the result may depend on the value of test, and that the class attribute of the result may be inappropriate for the values selected from yes and no. If you need an if statement for a single condition rather than a vector of them, see the if statement in R.
4. Use Vectorize() on a function that takes single values
The usage is Vectorize(FUN, vectorize.args = arg.names, SIMPLIFY = TRUE, USE.NAMES = TRUE). It returns, in the manual’s words, “a function with the same arguments as FUN, wrapping a call to mapply.”
pair_max = function(a, b) max(a, b)
max(c(1, 5, 3), c(4, 2, 6)) # 6 (one value for the whole input)
vpair_max = Vectorize(pair_max)
vpair_max(c(1, 5, 3), c(4, 2, 6)) # 4 5 6 (one value per pair)
Use vectorize.args to choose which arguments are vectorized. The manual’s own example does this with rep.int():
vrep = Vectorize(rep.int, "times")
vrep(times = 1:4, x = 42)
The arguments named in vectorize.args are the ones passed to mapply(). Only those that are actually passed are vectorized; default values are not. The manual illustrates this with a function whose x has a default: vf = Vectorize(function(x = 1:3, y) c(x, y), SIMPLIFY = FALSE), then vf(y = 1:3) vectorizes only y, not x.
5. Call mapply() directly when you need extra fixed arguments
The usage is mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE). Arguments are recycled if necessary, and MoreArgs is a list of other arguments to FUN that are not vectorized.
mapply(function(a, b, scale) (a + b) * scale,
1:3, 4:6,
MoreArgs = list(scale = 10)) # 50 70 90
Output and how to read it
- Built-in operations return a vector with one element per position (after recycling).
ifelse()returns a vector shaped liketest, not likeyesorno.Vectorize()andmapply()return “a list, or for SIMPLIFY = TRUE, a vector, array or list”. When every call returns a single value, you get a plain vector (as in step 4). When the calls return vectors of different lengths, you get a list: the manual’smapply(rep, 1:4, 4:1)example returns four vectors of lengths 4, 3, 2 and 1. SetSIMPLIFY = FALSEto always get a list.- Names: with
USE.NAMES = TRUE, the result uses the names of the first vectorized argument or, if that argument is an unnamed character vector, that vector as the names.
Which approach to use
| Situation | Use | What the documentation says |
|---|---|---|
| Arithmetic or a function that already works on whole vectors | The operation itself | R deals with entire vectors of data at a time (R Language Definition). |
| Choose between two values for each element | ifelse(test, yes, no) |
Result has the length and attributes of test. |
| Your own function takes single values and you want to pass vectors | Vectorize(FUN) |
Returns a wrapper around mapply(); default values are not vectorized. |
| Some arguments vary, others stay fixed | mapply(FUN, ..., MoreArgs = list(...)) |
MoreArgs holds the arguments that are not vectorized. |
| A function over a grid of two parameter vectors | Vectorize() then outer() |
The manual’s vSS example passes a vectorized function to outer(). |
| Each step depends on the previous result, or the work is not a per-element calculation | A loop, see for loops in R, or the map family, see map in R |
Covered in those guides. |
Troubleshooting
A recycling warning
This is the recycling warning from step 2: the longer vector’s length is not a multiple of the shorter one’s. Check that both inputs come from the same set of observations. A recycled result with no warning can still be wrong if the lengths only happen to divide evenly.
Vectorize() fails on a function such as rep
The manual states that Vectorize() cannot be used with primitive functions, because they do not have a value for formals. Its example uses rep.int because rep is primitive.
Only some arguments were vectorized
Default values are not vectorized, and only the arguments named in vectorize.args (all arguments of FUN by default) are. Pass the argument explicitly, or list it in vectorize.args.
A function has an argument named FUN, vectorize.args, SIMPLIFY or USE.NAMES
These names interfere with the arguments of Vectorize(). The manual’s workaround is to wrap the call in a function with a different argument name:
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)
ifelse() returns the wrong type or class
See the cautions in step 3: the result’s mode depends on test, and its class attribute may not suit the values taken from yes and no.
Free alternatives
Everything in this guide is part of base R. These are the base R tools that overlap with Vectorize():
| Tool | Part of | Best for |
|---|---|---|
| Built-in vectorized operators | base R | Element-wise arithmetic on vectors |
ifelse() |
base R | An element-wise yes/no choice |
mapply() |
base R | Several vectors, plus fixed extra arguments through MoreArgs |
Vectorize() |
base R | Turning a single-value function into one that takes vectors |
For the map family of functions, see map in R. Related guides on research software are collected in the research tools hub.
FAQ
What does Vectorize do in R?
Vectorize() creates a function wrapper that vectorizes the action of its argument FUN. The returned function has the same arguments as FUN and wraps a call to mapply(), so you can pass vectors to the arguments listed in vectorize.args.
Is Vectorize faster than a loop?
The manual describes Vectorize() as a wrapper that calls mapply() and makes no performance claim for it. Treat it as a convenience for passing vectors to a single-value function. The built-in vectorized operations, such as x + y, work on whole vectors directly and need no wrapper.
What is the difference between vectorized operations and Vectorize()?
Vectorized operations are how R’s built-in arithmetic and many functions already behave. Vectorize() is a function you call to give that behavior to a function that only handles single values.
Sources
- R Core Team, Vectorize: Vectorize a Scalar Function (base R manual, R 4.6.0), checked 2026-10-29.
- R Core Team, mapply: Apply a Function to Multiple List or Vector Arguments (base R manual), checked 2026-10-29.
- R Core Team, ifelse: Conditional Element Selection (base R manual), checked 2026-10-29.
- R Core Team, R Language Definition (sections on vectors, arithmetic and recycling), checked 2026-10-29.







