Appendix B — Answers for Section 4.7

  1. Create a numeric vector called vec1 comprising the elements 3, -12, 532, 0, -100, 55, and -42. Then, find the median and lowest value in the vector.
vec1 <- c(3, -12, 532, 0, -100, 55, -42) 

median(vec1)
[1] 0
min(vec1)
[1] -100
  1. Create a new vector vec2 which contains all the elements of vec1 that are positive integers. Then create a new vector vec3 that contains all the elements of vec1 that are negative integers. Then create a new vector vec4 which is the sum of vec2 and vec3.
vec2 <- vec1[vec1 > 0]
vec3 <- vec1[vec1 < 0]
vec4 <- vec2 + vec3
  1. Assign the built-in dataframe mtcars to an object named after your favorite animal. Then calculate and print the median of the variable mpg.
# I'm a big donkey guy, but you can put your favorite animal for the name of the object.
donkey <- mtcars 

median(donkey$mpg)
[1] 19.2
  1. Create a new variable and add to the object you created above (named after your favorite animal). This variable will take the variable Miles per Gallon mpg and convert it to Kilometers per Liter, which you will name kpl. This can be accomplished by taking mpg and dividing it by 2.352. Once you’ve created this new variable and added it to the object, calculate the median of kpl.
donkey$kpl <- donkey$mpg / 2.352
median(donkey$kpl)
[1] 8.163265
  1. Assign the built-in dataframe OrchardSprays to an object name of your choice. Then, convert the variable treatment to an ordered factor variable, and change the existing names of factor levels from A:H to a list of sulphur levels from Sulpher_8 to Sulpher_1. Then, print the new treatment variable.
library(dplyr)

# I'll assign the built-in OrchardSprays dataframe to an object called sprays.
sprays <- OrchardSprays

# Next, I'll convert the treatment variable to an ordered factor variable.

sprays$treatment <- factor(sprays$treatment,
                           ordered = TRUE)

summary(sprays$treatment)
A B C D E F G H 
8 8 8 8 8 8 8 8 
# Next, I'll replace the existing level names with new ones.
sprays$treatment <- recode(sprays$treatment,
                           A = "Sulphur_8",
                           B = "Sulphur_7",
                           C = "Sulphur_6",
                           D = "Sulphur_5",
                           E = "Sulphur_4",
                           F = "Sulphur_3",
                           G = "Sulphur_2",
                           H = "Sulphur_1")

print(sprays$treatment)
 [1] Sulphur_5 Sulphur_4 Sulphur_7 Sulphur_1 Sulphur_2 Sulphur_3 Sulphur_6
 [8] Sulphur_8 Sulphur_6 Sulphur_7 Sulphur_1 Sulphur_5 Sulphur_4 Sulphur_8
[15] Sulphur_3 Sulphur_2 Sulphur_3 Sulphur_1 Sulphur_8 Sulphur_4 Sulphur_5
[22] Sulphur_6 Sulphur_2 Sulphur_7 Sulphur_1 Sulphur_8 Sulphur_4 Sulphur_6
[29] Sulphur_3 Sulphur_2 Sulphur_7 Sulphur_5 Sulphur_4 Sulphur_5 Sulphur_2
[36] Sulphur_8 Sulphur_6 Sulphur_7 Sulphur_1 Sulphur_3 Sulphur_8 Sulphur_6
[43] Sulphur_3 Sulphur_2 Sulphur_7 Sulphur_5 Sulphur_4 Sulphur_1 Sulphur_7
[50] Sulphur_2 Sulphur_6 Sulphur_3 Sulphur_8 Sulphur_1 Sulphur_5 Sulphur_4
[57] Sulphur_2 Sulphur_3 Sulphur_5 Sulphur_7 Sulphur_1 Sulphur_4 Sulphur_8
[64] Sulphur_6
8 Levels: Sulphur_8 < Sulphur_7 < Sulphur_6 < Sulphur_5 < ... < Sulphur_1