# Vectors in R (part 2)

# 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:

baskets.of.Granny <- c(12, 4, 4, 6, 9, 3)

baskets.of.Granny [1] 12 4 4 6 9 3

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

sum(baskets.of.Granny) [1] 38

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:

str(baskets.of.Granny) [1] num [1:6] 12 4 5 6 9 3

- 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:

length(baskets.of.Granny) [1] 6

## Paste()

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:

firstnames <- c(“Joris”, “Carolien”, “Koen”)

lastnames <- “Meys”

paste(firstnames, lastnames) [1] “Joris Meys” “Carolien Meys” “Koen Meys”

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.

## Rep()

Is used for replicating the values in x.

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

rep(c(0, 0, 7), times = 4)[1] 0 0 7 0 0 7 0 0 7 0 0 7

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

rep(c(2, 4, 2), each = 2)[1] 2 2 4 4 2 2

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

rep(c(0, 7), times =c(4,3))[1] 0 0 0 0 7 7 7

## Any()

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

any(-2:2 < 0)

[1] Trueany(1:5 < 0)

[1] False

## All()

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

x <- 1:10all(x > 8)

[1] FALSEall(x > 0)

[1] TRUE

Suppose that R executes the following:

**any**(x > 5)

It first evaluates x > 5:

`FALSE, FALSE, FALSE, FALSE, FALSE)`

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