# Data structures

Author

Jeffrey R. Stevens

Published

February 6, 2023

## In-class coding

Make a sequence from 0 to 100 in steps of 10.

``seq(0, 100, 10)``
`` [1]   0  10  20  30  40  50  60  70  80  90 100``

Create a repetition of “yes” and “no” with 10 instance of each, alternating between the two. Then make one with 10 “yes” and then 10 “no”.

``rep(c("yes", "no"), times = 10)``
`````` [1] "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"
[13] "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no" ``````
``rep(c("yes", "no"), each = 10)``
`````` [1] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "no"  "no"
[13] "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no" ``````

Add the argument `n = 10` to `head(mtcars)`. What does this do?

``head(mtcars, n =  10)``
``````                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4``````

## Extra coding practice

### Vectors

Create a vector called `dog_names` with the values Bella, Daisy, and Max.

``dog_names <- c("Bella", "Daisy", "Max")``

Create a vector called `sex` with the values Female, Male, and Male.

``sex <- c("Female", "Female", "Male")``

Use the index operator to print to console only Daisy and Max from `dog_names`.

``dog_names[2:3]``
``[1] "Daisy" "Max"  ``

Replace the Daisy entry with Luna and print `dog_names` to console.

``````dog_names[2] <- "Luna"
dog_names``````
``[1] "Bella" "Luna"  "Max"  ``

### Lists

Copy/paste and run this code: `(mylist <- list(a = 1:4, b = c(4, 3, 8, 5), c = LETTERS[10:15], d = c("yes", "yes")))`

``(mylist <- list(a = 1:4, b = c(4, 3, 8, 5), c = LETTERS[10:15], d = c("yes", "yes")))``
``````\$a
[1] 1 2 3 4

\$b
[1] 4 3 8 5

\$c
[1] "J" "K" "L" "M" "N" "O"

\$d
[1] "yes" "yes"``````

Check the data types for each list element individually.

``typeof(mylist\$a)``
``[1] "integer"``
``typeof(mylist\$b)``
``[1] "double"``
``typeof(mylist\$c)``
``[1] "character"``
``typeof(mylist\$d)``
``[1] "character"``

Check the data types for each list element with one command.

``str(mylist)``
``````List of 4
\$ a: int [1:4] 1 2 3 4
\$ b: num [1:4] 4 3 8 5
\$ c: chr [1:6] "J" "K" "L" "M" ...
\$ d: chr [1:2] "yes" "yes"``````

Combine list elements `a` and `b` into a single vector.

``c(mylist\$a, mylist\$b)``
``[1] 1 2 3 4 4 3 8 5``

### Data frames

Create a data frame called `mydf` with three columns: `x`, `y`, and `z` and five rows. For `x` assign any five numbers, for `y` assign any five character strings, and for `z` assign any five logical values.

``(mydf <- data.frame(x = sample(1:10, 5, replace = TRUE), y = sample(letters, 5), z = sample(c(TRUE, FALSE), 5, replace = TRUE)))``
``````  x y     z
1 5 l  TRUE
2 3 c  TRUE
3 4 m FALSE
4 4 w  TRUE
5 5 j FALSE``````

Create a data frame called `dogs` that combines the `dog_names` and `sex` vectors and print to console.

``(dogs <- data.frame(dog_names, sex))``
``````  dog_names    sex
1     Bella Female
2      Luna Female
3       Max   Male``````

Print to console just Luna’s row.

``dogs[2, ]``
``````  dog_names    sex
2      Luna Female``````

Print to console the number of rows in `dogs`.

``nrow(dogs)``
``[1] 3``