Question 1

Set the vector v1 equal to the following: 11, 13, 15, 17, 19, …, 99, 101.

 v1 <- seq(11, 101, by = 2)

v1
##  [1]  11  13  15  17  19  21  23  25  27  29  31  33  35  37  39  41  43  45  47
## [20]  49  51  53  55  57  59  61  63  65  67  69  71  73  75  77  79  81  83  85
## [39]  87  89  91  93  95  97  99 101

Question 2

Set the vector v2 equal to the following: “A” “A” “B” “B” “C” “C” “D” “D” “E” “E” (note the letters are all uppercase).

v2 <- c("A","A","B","B","C","C","D","D","E","E")

v2
##  [1] "A" "A" "B" "B" "C" "C" "D" "D" "E" "E"

Question 3

Set the vector v3 equal to the words “dog” 10 times, “cat” 9 times, “fish” 6 times, and “ferret” 1 time. (Here, you will have to check documentation for the rep() function, and/or the textbook.)

v3 <- c(rep("dog", 10),
        rep("cat", 9),
        rep("fish", 6),
        rep("ferret", 1))

v3
##  [1] "dog"    "dog"    "dog"    "dog"    "dog"    "dog"    "dog"    "dog"   
##  [9] "dog"    "dog"    "cat"    "cat"    "cat"    "cat"    "cat"    "cat"   
## [17] "cat"    "cat"    "cat"    "fish"   "fish"   "fish"   "fish"   "fish"  
## [25] "fish"   "ferret"

Question 4a

Use map() functions to create a list of 10 vectors of 100 numbers sampled from 10 random normal distributions with means of 1 to 10 (in steps of 1) and SDs of 5. Assign this list to the object samples.

NOTE: In map(), you can define arguments of the function after providing the name of the function. The vector/list provided will iterate across the first argument that is NOT provided.

set.seed(321) # ensures exact reproducibility

samples <- purrr::map(1:10, rnorm, n = 100, sd = 5)

glimpse(samples)
## List of 10
##  $ : num [1:100] 9.525 -2.56 -0.39 0.402 0.38 ...
##  $ : num [1:100] -4.52 -2.59 7.67 6.09 -3.87 ...
##  $ : num [1:100] -3.499 13.599 5.727 3.777 -0.428 ...
##  $ : num [1:100] -0.74 -6.08 -4.68 14.72 1.62 ...
##  $ : num [1:100] 4.024 7.473 6.216 -1.377 0.559 ...
##  $ : num [1:100] 1.73 4.685 -0.392 12.4 13.367 ...
##  $ : num [1:100] 3.038 5.423 17.136 12.216 0.745 ...
##  $ : num [1:100] 9.04 6.25 15.95 12.5 6.71 ...
##  $ : num [1:100] 4.966 8.36 8.174 12.382 -0.157 ...
##  $ : num [1:100] 8.574 3.255 0.734 21.196 9.338 ...

Question 4b

Use map() functions to create a vector of the sample means from the list samples in the previous question. Then, specifically do the same thing but create a vector of the sample means in which the means are character strings.

sample_means <- purrr::map_dbl(samples, mean)
sample_means_char <- purrr::map_chr(samples, ~as.character(mean(.x)))

glimpse(sample_means)
##  num [1:10] 1.05 1.99 3.82 4.42 5.52 ...
glimpse(sample_means_char)
##  chr [1:10] "1.04533834298671" "1.99224355267195" "3.81900699890789" ...

Question 5

Write a for loop that produces the exact same output as the output in 4a. Use glimpse to look at both lists at once, to compare them.

NOTE: Look carefully at the for loop in the slides.

set.seed(321)

samples_for <- list()

for(i in 1:10){
  samples_for[[i]] <- rnorm(n = 100, mean = i, sd = 5)
}

glimpse(samples)
## List of 10
##  $ : num [1:100] 9.525 -2.56 -0.39 0.402 0.38 ...
##  $ : num [1:100] -4.52 -2.59 7.67 6.09 -3.87 ...
##  $ : num [1:100] -3.499 13.599 5.727 3.777 -0.428 ...
##  $ : num [1:100] -0.74 -6.08 -4.68 14.72 1.62 ...
##  $ : num [1:100] 4.024 7.473 6.216 -1.377 0.559 ...
##  $ : num [1:100] 1.73 4.685 -0.392 12.4 13.367 ...
##  $ : num [1:100] 3.038 5.423 17.136 12.216 0.745 ...
##  $ : num [1:100] 9.04 6.25 15.95 12.5 6.71 ...
##  $ : num [1:100] 4.966 8.36 8.174 12.382 -0.157 ...
##  $ : num [1:100] 8.574 3.255 0.734 21.196 9.338 ...
glimpse(samples_for)
## List of 10
##  $ : num [1:100] 9.525 -2.56 -0.39 0.402 0.38 ...
##  $ : num [1:100] -4.52 -2.59 7.67 6.09 -3.87 ...
##  $ : num [1:100] -3.499 13.599 5.727 3.777 -0.428 ...
##  $ : num [1:100] -0.74 -6.08 -4.68 14.72 1.62 ...
##  $ : num [1:100] 4.024 7.473 6.216 -1.377 0.559 ...
##  $ : num [1:100] 1.73 4.685 -0.392 12.4 13.367 ...
##  $ : num [1:100] 3.038 5.423 17.136 12.216 0.745 ...
##  $ : num [1:100] 9.04 6.25 15.95 12.5 6.71 ...
##  $ : num [1:100] 4.966 8.36 8.174 12.382 -0.157 ...
##  $ : num [1:100] 8.574 3.255 0.734 21.196 9.338 ...