Vectorizing your functions

Vectorized functions are a very useful feature of R, but programmers who are used to other languages often have trouble with this concept at first. A vectorized function works not just on a single value, but on a whole vector of values at the same time. To try vectorized functions, you have to make a vector. You do this by using the c() function, which stands for combine. The actual values are separated by commas.

Here’s an example: Suppose that Granny plays basketball with her friend Geraldine, and you keep a score of Granny’s number of baskets in each game. After six games, you want to know how many baskets Granny has made so far this season. You can combine these numbers into a vector, like this:

To find the total number of baskets Granny made, you just type the following:

You could get the same result by going over the vector number by number, adding each new number to the sum of the previous numbers. But that method would require you to write more code and it would take longer to calculate. You won’t notice it on just six numbers, but the difference will be obvious when you have to sum a few thousand of them. Actually, this kind of vectorization occurs in many programming languages. Functions that work this way summarize the data in a vector; they take all values in the vector and calculate a single result. R also can carry out functions along vectors. This type of vectorization is pretty unique, and forms the core of R’s incredible power.

Structure of a vector

R gives you an easy way to look at the structure of any object. This method comes in handy whenever you doubt the form of the result of a function or a script you wrote. To take a peek inside R objects, use the str() function. The str() function gives you the type and structure of the object. Take a look at the vector baskets.of.Granny:

  • First, it tells you that this is a num (numeric) type of vector.
  • Next to the vector type, R gives you the dimensions of the vector. This example has only one dimension, and that dimension has indices ranging from 1 to 6.
  • Finally, R gives you the first few values of the vector. In this example, the vector has only six values, so you see all of them.

If you want to know only how long a vector is, you can simply use the length() function, as follows:


To see how it works, try using the paste() function. First, you construct two vectors (for example, a vector with first names and a vector with last names). To create a vector with the full names from the original vectors, you can simply use the paste() function, like this:

R automatically loops over the values of each vector, and concatenates (pastes) them together, element by element. So the first value of the vector firstnames is pasted to the first value of lastnames, the second value of firstnames to the second of lastnames, and so forth.


Is used for replicating the values in x.

To repeat the vector c(0, 0, 7) three times, use this code:

We can also repeat every value by specifying each argument, like this:

c) For each value, we can tell R how often it has to repeat:


It takes the set of vectors and returns a set of logical vectors, in which at least one of the value is true.


It takes the set of vectors and returns a set of logical vectors, in which all of the values are TRUE.

Suppose that R executes the following:

It first evaluates x > 5:

any() function reports whether any of those values are TRUE, while all() function works and reports if all the values are TRUE.