# Lesson 4a - Vectors

Collections are items that contain other items. In R, vectors, matrices, lists, and data frames are four types of collections. In this lesson, we’ll cover vectors.

## Table of Contents

## Lesson Objectives

- Use vectors to group values of the same data type
- Access values in vectors

## What’s a Vector?

Vectors are one of the four types of collections we will cover. They contain elements of the same type, so you can have a vector of logicals, a vector of strings, and so on.

## Creating a Vector

The format to create a vector is the following:

```
myVector <- c(item1, item2, item3, ...)
```

Don’t forget that all items in a vector must be of the same type.

Input

```
myVector <- c(1, 2.0, 6, -4)
myVector2 <- c(1, 4.2, 3.2, "hello")
myVector
myVector2
```

Output

```
[1] 1 2 6 -4
[1] "1" "4.2" "3.2" "hello"
```

In the example above, you’ll notice that in `myVector2`

, all the numeric data values are turned into strings. Rather than throwing an error, R will do what it think is most appropriate and turn all the values into one data type.

### Creating a Vector of Consecutive Numbers

You can use the `:`

operator to create a vector of consecutive numbers.

Input

```
x <- 1:10
x
```

Output

```
[1] 1 2 3 4 5 6 7 8 9 10
```

## Adding an Element to a Vector

The `c()`

function can also add elements to an existing vector.

Input

```
myVector <- c(1, 2.0, 6, -4)
myVector <- c(myVector, -6)
myVector
```

Output

```
[1] 1 2 6 -4 -6
```

## Concatenating Vectors

To concatenate vectors, you can use the same `c()`

function.

Input

```
myVector <- c(1, 2.0, 6, -4)
myVector2 <- c(5, 6, 7, 8)
myVector3 <- c(myVector, myVector2)
myVector3
```

Output

```
[1] 1 2 6 -4 5 6 7 8
```

## Storing Values by Name

You can also store individual values in a vector by name (usually referred to as a key), which will be useful when indexing values (coming up in the next section).

Input

```
myVector <- c(first = 1, second = 2, third = 3, fourth = 4)
myVector
```

Output

```
first second third fourth
1 2 3 4
```

## Accessing Vector Contents

There are several ways to access the individual contents of a vector.

Just like with strings, in R, indexing starts at 1.

```
myVector <- c(a = "apple", b = "banana", c = "cow", d = "donkey", e = "elephant")
myVector[3] # This returns the third value in the vector.
myVector[2:4] # This returns every value starting from the second value until the fourth value. (inclusive)
myVector[c(1, 3, 4)] # This returns specific values #1, #3, and #4.
myVector[-2] # This returns all items excluding the second item.
myVector[-2:-4] # This returns all items excluding all values from the second item until the fourth item. (inclusive)
myVector[c(-2, -4)] # This returns all items excluding the speicifc values #2 and #4.
# The following lines access vectors with items that are named
myVector['a'] # This returns the value with the name 'a'
myVector[c('a', 'b')] # This returns the values with the name 'a' and 'b'
# You can also index all elements that fulfil a condition
myVector[ grepl("le", myVector, fixed=TRUE) ] # This returns `a` and `e`, because they both contain "le".
```

Try these in RStudio and experiment, the best way to learn R is to code!

## Modifying a Value in a Vector

To modify the value at a specific, index the value as usual and assign a value to it just like you would a regular variable.

Input

```
myVector <- c(3, 4, 5)
myVector[3] <- 6
myVector
```

Output

```
[1] 3 4 6
```

## Key Points / Summary

- You can use vectors to make a collection of values.
- Values in a vector must be of the same data type.
- You can access and modify values in a vector using indexing.
- Indexes in R start at 1.