1 Load package

Load package for class exercises:

library(tidyverse)

2 Exercise 1

Create the following vectors

  1. a sequence of even numbers from 2 to 20
  2. 1 to 9 twice
  3. 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7
  4. the word “fish” 18 times
  5. the words “blue”, “white”, and “yellow” six times
# 1) a sequence of even numbers from 2 to 20
seq(2, 20, 2)
##  [1]  2  4  6  8 10 12 14 16 18 20
# 2) 1 to 9 twice
rep(1:9, 2) 
##  [1] 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
# 3) 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7
rep(2:7, each = 3)
##  [1] 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7
# 4) the word "fish" 18 times
rep("fish", 18)
##  [1] "fish" "fish" "fish" "fish" "fish" "fish" "fish" "fish" "fish" "fish"
## [11] "fish" "fish" "fish" "fish" "fish" "fish" "fish" "fish"
# 5) the words "blue", "white", and "yellow" six times
rep(c("blue", "white", "yellow"), each = 6)
##  [1] "blue"   "blue"   "blue"   "blue"   "blue"   "blue"   "white"  "white" 
##  [9] "white"  "white"  "white"  "white"  "yellow" "yellow" "yellow" "yellow"
## [17] "yellow" "yellow"

3 Exercise 2

  1. create a vector with random integer values using rpois(100, 10)
  2. create a second vector with values that are smaller or equal to 10 but not 5
  3. sort the second vector in ascending order
  4. calculate the mean and standard deviations of the second vector
  5. calculate the maximum value of the second vector
# 1) create a vector with random integer values using rpois(100, 10)
vector <- rpois(100, 10)
vector
##   [1] 10 12  9  5  7 11  7 10 12 11 13 12  8  7  9  9 16  9  6  7 10  7 13  9 11
##  [26] 10  9 17 13 14 10 21 10  6 14 12 11 10 10 10  8  9 11 11 11  9  9  5 21  8
##  [51]  9 10 11 12  7  9 12 18  8 14  8 11 15  8 13  8 18  8 12  9 12  9  9  7 13
##  [76] 10 12 10  8 11  7 11 14  6  7 14 10 12 13 10  9  7 12  6  7 12  9  9 13 12
# 2) calculate the number of values that are smaller or equal to 10 but not 5
vector2 <- vector[vector <= 10 & vector != 5]
vector2
##  [1] 10  9  7  7 10  8  7  9  9  9  6  7 10  7  9 10  9 10 10  6 10 10 10  8  9
## [26]  9  9  8  9 10  7  9  8  8  8  8  8  9  9  9  7 10 10  8  7  6  7 10 10  9
## [51]  7  6  7  9  9
# 3) sort the vector in ascending order 
sort(vector2)
##  [1]  6  6  6  6  7  7  7  7  7  7  7  7  7  7  7  8  8  8  8  8  8  8  8  8  9
## [26]  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9 10 10 10 10 10 10 10 10 10
## [51] 10 10 10 10 10
# 4) calculate the mean and standard deviations 
mean(vector2)
## [1] 8.472727
sd(vector2)
## [1] 1.274458
# 5) calculate the minimum and maximum values
max(vector2)
## [1] 10
min(vector2)
## [1] 6

4 Exercise 3

  1. create your own matrix
  2. convert it to a data frame
  3. give your data frame row and column names
  4. convert it to a tibble
# 1) create your own matrix
mx <- matrix(rpois(30, 5), nrow = 5)
mx
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    4    6    2    7    5    7
## [2,]    5    3    5    7    7    5
## [3,]    4    6    1    9    6    4
## [4,]    2    4    5    3    3    6
## [5,]    8    3    5    4    9    5
# 2) convert it to a data frame
df <- as.data.frame(mx)
df
##   V1 V2 V3 V4 V5 V6
## 1  4  6  2  7  5  7
## 2  5  3  5  7  7  5
## 3  4  6  1  9  6  4
## 4  2  4  5  3  3  6
## 5  8  3  5  4  9  5
# 3) give your data frame row and column names
colnames(df) <- c("african.penguin", "little.penguin", "rockhopper.penguin", "emperor.penguin", "chinstrap.penguin", "adelie.penguin")
rownames(df) <- c("site1", "site2", "site3", "site4", "site5")
df
##       african.penguin little.penguin rockhopper.penguin emperor.penguin
## site1               4              6                  2               7
## site2               5              3                  5               7
## site3               4              6                  1               9
## site4               2              4                  5               3
## site5               8              3                  5               4
##       chinstrap.penguin adelie.penguin
## site1                 5              7
## site2                 7              5
## site3                 6              4
## site4                 3              6
## site5                 9              5
# 4) convert it to a tibble
tb <- as_tibble(df)
tb
## # A tibble: 5 × 6
##   african.penguin little.penguin rockhopper.penguin emperor.pe…¹ chins…² adeli…³
##             <int>          <int>              <int>        <int>   <int>   <int>
## 1               4              6                  2            7       5       7
## 2               5              3                  5            7       7       5
## 3               4              6                  1            9       6       4
## 4               2              4                  5            3       3       6
## 5               8              3                  5            4       9       5
## # … with abbreviated variable names ¹​emperor.penguin, ²​chinstrap.penguin,
## #   ³​adelie.penguin

5 Exercise 4

  1. download the dataset called “coralreefherbivores.csv” from Canvas, and put it in your working directory in a folder called “data”
  2. read in the data using the read.csv() function, specifying correct path to the “data” folder
  3. calculate the mean of the column “snoutlength”
# 1) read in the data using the read.csv() function, specifying correct path to the "data" folder
data <- read.csv(file = "data/coralreefherbivores.csv")

# 2) calculate the mean of the column "snoutlength"
mean(data$snoutlength)
## [1] 0.4311323