Solution to working with vectors

You have the following three vectors:

species <- c(
  "MountainBeaver", "Cow", "GreyWolf", "Goat",
  "GuineaPig", "Diplodocus", "AsianElephant", "Donkey",
  "Horse", "PotarMonkey", "Cat", "Giraffe",
  "Gorilla", "Human", "AfricanElephant", "Triceratops",
  "RhesusMonkey", "Kangaroo", "GoldenHamster", "Mouse",
  "Rabbit", "Sheep", "Jaguar", "Chimpanzee",
  "Rat", "Brachiosaurus", "Mole", "Pig"
)

bodywt_kg <- c(
  1.4, 465, 36.3, 27.7, 1., 11700, 2547, 187.1,
  521, 10, 3.3, 529, 207, 62, 6654, 9400,
  6.8, 35, 0.1, 0.02, 2.5, 55.5, 100, 52.2,
  0.3, 87000, 0.1, 192
)

brainwt_kg <- c(
  0.0081, 0.423, 0.1195, 0.115, 0.0055, 0.05, 
  4.603, 0.419, 0.655, 0.115, 0.0256, 0.68, 
  0.406, 1.32, 5.712, 0.07, 0.179, 0.056,
  0.001, 0.0004, 0.0121, 0.175, 0.157, 0.44,
  0.0019, 0.1545, 0.003, 0.18
)
animals_to_check <- c("Snail", "Goat", "Chimpanzee", "Rat", "Dragon", "Eagle")

# Goes through every element in animals_to_check and returns TRUE
# if it appears in species
animals_to_check %in% species
[1] FALSE  TRUE  TRUE  TRUE FALSE FALSE

You can see that Goat, Chimpanzee and Rat are contained in species.

To see the actual species name instead of just TRUE and FALSE, you have to index the vector:

animals_to_check[animals_to_check %in% species]
[1] "Goat"       "Chimpanzee" "Rat"       
Note

If you use %in% to check if elements are contained in a vector, the result vector is of the same length as the vector that is before the %in%.

Compare the result from above with the following:

# Goes through every element in species and returns TRUE
# if it appears in animals_to_check
species %in% animals_to_check
 [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
[25]  TRUE FALSE FALSE FALSE
mean(brainwt_kg)
[1] 0.5745214
sd(brainwt_kg)
[1] 1.334929
species[brainwt_kg > mean(brainwt_kg)]
[1] "AsianElephant"   "Horse"           "Giraffe"         "Human"          
[5] "AfricanElephant"
brain_body_ratio <- brainwt_kg / bodywt_kg * 100

Step 1: Look at the brain to body ratio of humans

brain_body_ratio[species == "Human"]
[1] 2.129032

Step 2: Compare it with the other animals

# New variable for human brain to body ratio
bbr_human <- brain_body_ratio[species == "Human"]
# Are there animals that have a larger brain to body ratio than humans?
brain_body_ratio > bbr_human
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE  TRUE FALSE
# Which are these animals
species[brain_body_ratio > bbr_human]
[1] "RhesusMonkey" "Mole"        
# or short
species[brain_body_ratio > brain_body_ratio[species == "Human"]]
[1] "RhesusMonkey" "Mole"        

1 Extras

  • Round the vector to 4 decimal places:
brain_body_ratio <- round(brain_body_ratio, digits = 4)
brain_body_ratio
 [1] 0.5786 0.0910 0.3292 0.4152 0.5500 0.0004 0.1807 0.2239 0.1257 1.1500
[11] 0.7758 0.1285 0.1961 2.1290 0.0858 0.0007 2.6324 0.1600 1.0000 2.0000
[21] 0.4840 0.3153 0.1570 0.8429 0.6333 0.0002 3.0000 0.0938
  • Which animal has the smallest brain to body ratio?
species[brain_body_ratio == min(brain_body_ratio)]
[1] "Brachiosaurus"

The Brachiosaurus has a really small brain with a weight of just 0.1545 kg at a body weight of 8.7^{4} kg!

  • Add elements to the vector
species_new <- c("Eagle", "Snail", "Lion")
brainwt_kg_new <- c(0.0004, NA, 0.5)
bodywt_kg_new <- c(18, 0.01, 550)

species <- c(species, species_new)
brainwt_kg <- c(brainwt_kg, brainwt_kg_new)
bodywt_kg <- c(bodywt_kg, bodywt_kg_new)
  • What is the mean brain weight now?
mean(brainwt_kg)
[1] NA
# na.rm = TRUE removes missing values before calculating the mean
mean(brainwt_kg, na.rm = TRUE)
[1] 0.5529