Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Key elements of the visualization process
Environment - the R Language
15. Vectors in R

## Instruction

The vector data type lets us store more than one value in one variable. In technical jargon, vectors are a collection of values of the same type. Vectors are created by the function c().

If you want to save five numbers representing the ages of your five family members, you create a vector for them. If their ages are 12, 15, 22, 52, and 53, you'd write:

ages <- c(12, 15, 22, 52, 53)

You've created a 5-element numerical vector. Note that all the ages go inside the parentheses after c, and all of them are separated by a comma.

If you want access to particular element of a vector, you use the vector name and the position of the element, enclosed in brackets. So to look up the first age in your ages vector, you'd write:

ages[1]

This will return 12, the first in your list of ages.

Remember that vector inherits the type of its elements, so it must contain elements of the same data type. You can have numerical vectors, logical vectors, or character vectors (containing the string data type).

Apart from storing many values in one variable, vectors offer another advantage: you can simultaneously change all the values in them. For example, if you want to add one year to each of your family members' ages, you can simply type:

ages <- ages + 1

This will change the ages to 13, 16, 23, 53, and 54.

Good to Know: Most R functions will work on vectors. In other words, they will transform every element in the same way at the same time.

## Exercise

In the zoo, you have four penguins named Suzanne, Marty, John, and Nina. Save these names in the penguinNames variable. (Don't forget to add quotes before and after each name!).

When you're done, press .

### Stuck? Here's a hint!

You should write:

penguinNames <- c("Suzanne", "Marty", "John", "Nina")