---
title: Getting started with R
author: YOUR NAME HERE
date: today
format: pdf
execute:
  fig-height: 4
  message: false
  warning: false
  error: true
---

This lesson is designed to explain the basics of how R works as a programming language. 

## R as a calculator 

> put your code chunk here then remove this text

## Creating objects in R

```{r}
weight_kg <- ______
```

#### Names are important

```{r}
weight_kg <- 55    # doesn't print anything
(____________)  # but putting parenthesis around the call prints the value of `weight_kg`
______          # and so does typing the name of the object
```

Convert this weight into pounds (weight in pounds is 2.2 times the weight in kg):

```{r}
______ * weight_kg
```

We can also change an object's value by assigning it a new one:

```{r}
weight_kg <- ______
2.2 * ______
```

For example, let's store the animal's weight in pounds in a new object, `weight_lb`:

```{r}
weight_lb <- ______
```

and then change `weight_kg` to 100.

```{r}
weight_kg <- ______
```

#### Comment your code

```{r}
______ <- 57.5 # enter the weight in kg
weight_lb <- ______ # convert it to lbs
# comments can go nearly anywhere in a code chunk
```


## Functions and their arguments

```{r}
______(4)
```

Let's look into the `round` function. 

```{r}
round(______)
```

We can learn more about this function by typing `?round`. 

#### Your Turn

Use the example in the help file to round the digits of pi to 2 digits.

```{r}
round(pi, digits = ______)
```

## Data Types

R objects come in different data types. You can use the function `class()` to see what data type an object is. 

```{r}
______(weight_kg)
```

### Numbers

When a number is stored in an object it is now called a **numerical** variable. We can do math on numeric variables. 

```{r}
im_a_number <- 50
class(______)
______*2
```


### Letters


```{r}
(im_a_character <- "dog")
______(im_a_character)
______*2
```


### Boolean
Silly examples include "Is 3 greater than 4?" and "Is the square root of 4 equal to 2?"

```{r}
(huh <- ______)
______(huh)
sqrt(4)______2
```


## Data Structures
### Vectors 

```{r}
(______ <- ______(25, 250, 7800, 3600))
```

A vector can also contain characters:

```{r}
(______ <- ______("mouse", "rat", "dog", "cat"))
```

An important feature of a vector, is that all of the elements are the same type of data. 
```{r}
______(weight_g)
class(______)
```

If you combine letters and numbers, everything will be treated as letters. 
```{r}
(mix_match <- c(______, ______))
class(______)
```

### Subsetting vectors

If we want to extract one or several values from a vector, we must provide one or several indices in square brackets. For instance:

```{r}
animals[______]
animals______
```

The number in the indices indicates which element to extract. For example we can extract the 3rd element in `weight_g` by typing

```{r}
______[3]
```

### Conditional subsetting 

Another common way of subsetting is by using a logical vector. 

```{r}
weight_g ____  # Returns TRUE or FALSE for each element in the vector
```

**Which animals weigh over 1000g?**
```{r}
______[weight_g ______ ]
```
