R For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon

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.

About This Article

This article is from the book:

About the book authors:

Andrie de Vries is a leading R expert and Business Services Director for Revolution Analytics. With over 20 years of experience, he provides consulting and training services in the use of R. Joris Meys is a statistician, R programmer and R lecturer with the faculty of Bio-Engineering at the University of Ghent.

This article can be found in the category: