How to Combine Vectors in R

By Andrie de Vries, Joris Meys

To dive a bit deeper into how you can use vectors in R, let’s consider this All-Star Grannies example. You have two vectors that contain the number of baskets that Granny and her friend Geraldine scored in the six games of this basketball season:

> baskets.of.Granny <- c(12, 4, 4, 6, 9, 3)
> baskets.of.Geraldine <- c(5, 3, 2, 2, 12, 9)

The c() function stands for combine. It doesn’t create vectors — it just combines them.

You give six values as arguments to the c() function and get one combined vector in return. As you know, R considers each value a vector with one element. You also can use the c() function to combine vectors with more than one value, as in the following example:

> all.baskets <-c(baskets.of.Granny, baskets.of.Geraldine)
> all.baskets
 [1] 12 4 4 6 9 3 5 3 2 2 12 9

The result of this code is a vector with all 12 values.

In this code, the c() function maintains the order of the numbers. This example illustrates a second important feature of vectors: Vectors have an order. This order turns out to be very useful when you need to manipulate the individual values in the vector.