## 1.3 Lab Activity

### 1.3.1 Mathematical Operations

Write R code to solve each of these equations:

1. $$1/20$$
2. $$\frac{1}{20 \times 20}$$
3. $$1 - .95^{10}$$
4. $$\sqrt{19}$$

### 1.3.2 Object Assignment

Use the assignment operator, <-, to define R objects with the names and values shown in the table below.

name value
x 10
y 4.2
haiku a haiku about R
excited logical, honest answer to the question, “I am excited to learn R”

### 1.3.3 Object Classes

Use class() to identify the class(es) of each of the objects you just created.

### 1.3.4 Longer Vectors

A vector is a list of items of the same type. Vectors can contain numbers, characters, or logicals, so long as every item in that vector is the same class. The objects you have created thus far are all vectors of length one. That is, they each contain one item. But vectors more often contain many items.

You can combine items into a vector using c(), which is the concatenate/combine function. This function takes any number of arguments, each separated by a comma, and combines them into a vector.

Use the assignment operator, <-, and the concatenate function, c() to define R objects with the names and values shown in the table below.

name value
dub a numeric/double vector of length 3
dub2 a numeric/double vector of length 4

You can also name elements in your vector, as in the example below.

ages <- c("Declan" = 17, "Ava" = 19, "Liam" = 20, "Charlotte" = 19)

For longer function calls, it is easier to read if they are spread out over several lines. The code below is formatted differently, but functionally identical to the code which used only one line.

ages <- c(
"Declan" = 17,
"Ava" = 19,
"Liam" = 20,
"Charlotte" = 19
)
ages
##    Declan       Ava      Liam Charlotte
##        17        19        20        19

This can be useful, but a more common approach is to use data frames.

### 1.3.5 Data Frames

A data.frame is a special class of R object that groups vectors into columns. This is a useful way of organizing and presenting data. It is easy to read and understand. The data we will work with this semester will be organized in a data.frame.

Create a data.frame using the function data.frame(). Like c(), data.frame(), combines its arguments. However, instead of combining elements into a vectors, data.frame() binds vectors into columns of a table.

data.frame(
name = c("Declan", "Ava", "Liam", "Charlotte"),
age = c(17, 19, 20, 19)
)
##        name age
## 1    Declan  17
## 2       Ava  19
## 3      Liam  20
## 4 Charlotte  19

This data.frame has the same information as the vector ages we created previously. Except the information is stored in two vectors—name and age—instead of one named vector. The ability to store information this way becomes increasingly useful if we want to more information about these individuals, as in the example below.

Recreate the following in R. Save the result as an object named happiness.

##        name age group shs_1 shs_2 shs_3
## 1    Declan  17     A     3     4     4
## 2       Ava  19     A     4     3     3
## 3      Liam  20     B     5     5     4
## 4 Charlotte  19     B     4     5     5

### 1.3.6 Add a Column to a Data Frame

Use \$ to add a column called shs_total to happiness, which is the sum of shs_1, shs_2, and shs_3. The result should look like this.

##        name age group shs_1 shs_2 shs_3 shs_total
## 1    Declan  17     A     3     4     4        11
## 2       Ava  19     A     4     3     3        10
## 3      Liam  20     B     5     5     4        14
## 4 Charlotte  19     B     4     5     5        14

### 1.3.7 Summary Statistics

Use R functions to compute the mean, median, and standard deviation of shs_total.