## What is a "mode"?

In `R`, individual elements (the smallest unit that we store) have a "mode", which describes the type of quantity they describe. Possible modes include:

- Integer
- Numeric (Floating Point, Double)
- Character (String)
- Logical (Boolean)
- Complex

We usually don't need to tell `R` what the mode should be when we define something. It guesses from what we provide. 

We can use the `str` and `typeof` functions to learn what the mode of a variable we have defined is.

## Numeric (Floating Point, Double)

```{r, eval = TRUE, echo = TRUE}
x <- 1.1
str(x)
```

```{r, eval = TRUE, echo = TRUE}
typeof(x)
```

```{r, eval = TRUE, echo = TRUE}
mode(x)
```


## Character (String)

```{r, eval = TRUE, echo = TRUE}
x <- "a"
str(x)
```

```{r, eval = TRUE, echo = TRUE}
typeof(x)
```

```{r, eval = TRUE, echo = TRUE}
mode(x)
```

Note: Characters can include more than one element, e.g. `x <- "abc"`.

## Logical (Boolean)

```{r, eval = TRUE, echo = TRUE}
x <- TRUE
str(x)
```

```{r, eval = TRUE, echo = TRUE}
typeof(x)
```

```{r, eval = TRUE, echo = TRUE}
mode(x)
```

The logical mode can take on values `TRUE` and `FALSE`, which can be abbreviated `T` and `F`.

Note: For this reason, naming variables `T` or `F` is discouraged.

## Integer?

```{r, eval = TRUE, echo = TRUE}
x <- 1
str(x)
```

```{r, eval = TRUE, echo = TRUE}
typeof(x)
```

```{r, eval = TRUE, echo = TRUE}
mode(x)
```

If `R` has to guess whether a number is an integer or a numeric, it will default to numeric.

## Integer!

We actually do need to tell `R` the mode when we want to define an integer. A way to do that is to apply the function `as.integer` to the integer we provide.

```{r, eval = TRUE, echo = TRUE}
x <- as.integer(1)
str(x)
```

```{r, eval = TRUE, echo = TRUE}
typeof(x)
```

```{r, eval = TRUE, echo = TRUE}
mode(x)
```

## Vectors

Vectors are collections of elements that share the same mode. 

The length of a vector describes the number of elements in a vector. 

In fact, everything we've seen so far was a vector of length $1$!

```{r, eval = TRUE, echo = TRUE}
x <- 1
str(x)
```

## Creating a Vector

We can construct vectors from multiple elements using the `c` function, where `c` stands for **concatenate**. 

```{r, eval = TRUE, echo = TRUE}
x <- c(1, 5, 2)
```

```{r, eval = TRUE, echo = TRUE}
str(x)
```

```{r, eval = TRUE, echo = TRUE}
x
```

## Determining the Number of Elements in a Vector

The `length` function, when applied to a vector, returns the number of elements in a vector.

```{r, eval = TRUE, echo = TRUE}
length(x)
```

## Viewing an Element of a Vector

```{r, eval = TRUE, echo = TRUE}
x[1]
```

```{r, eval = TRUE, echo = TRUE}
x[2]
```

```{r, eval = TRUE, echo = TRUE}
x[3]
```

## Viewing Elements of a Vector

```{r, eval = TRUE, echo = TRUE}
x[1:2]
```

```{r, eval = TRUE, echo = TRUE}
x[c(1, 3)]
```

```{r, eval = TRUE, echo = TRUE}
x[-2]
```

```{r, eval = TRUE, echo = TRUE}
x[-c(1, 3)]
```

## Growing a Vector

Unlike some other languages, `R` allows you to make a vector longer or make it shorter.

```{r, eval = TRUE, echo = TRUE}
x <- c(x, 4)
```

```{r, eval = TRUE, echo = TRUE}
x
```


## Shortening a Vector


```{r, eval = TRUE, echo = TRUE}
x <- x[1:3]
```

```{r, eval = TRUE, echo = TRUE}
x
```

## Replacing an Element of a Vector

```{r, eval = TRUE, echo = TRUE}
x[2] <- 5.1
```

```{r, eval = TRUE, echo = TRUE}
x
```

## Looping Over Elements of a Vector

It is common that we may want to apply a function to one element of a vector at a time. 

```{r, eval = TRUE, echo = TRUE}
for (i in 1:length(x)) {
  x[i] <- i
}
```

```{r, eval = TRUE, echo = TRUE}
x
```

## Operations on Vectors - Addition

```{r, eval = TRUE, echo = TRUE}
x <- c(1, 4)
y <- c(9.2214, 0.12)
x + y
```

## Operations on Vectors - Recycling

```{r, eval = TRUE, echo = TRUE}
y <- y[1]
x + y
```