Introduction to R

Slides in full screen     Download PDF slides

1 Task

Note

Find the solution here.

You have the following three vectors:

  • species: name of the species
  • bodywt_kg: body weight of the species in kg
  • brainwt_g: brain weight of the species in g
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
)

Copy and paste the vectors into your R script and solve the following tasks.

  • Check which of the following animals are contained in the species vector:
animals_to_check <- c("Snail", "Goat", "Chimpanzee", "Rat", "Dragon", "Eagle")
  • Calculate mean and standard deviation of the brain weight
    • Hint: have a look at the summary slides to find the functions
  • Which species have a brain weight larger than the mean brain weight of all species?
  • Calculate the ratio of brain weight to body weight in percent for all animals and save the result in a new vector
  • A bit more tricky: Are there any animals with a larger brain to body weight ratio than humans? If yes, which ones?
    • Hint: calculate the ratio for humans and save it in a separate variable first

1.1 Extras

  • Round the ratio vector to 4 decimal places with the round function
    • Type ?round into the console to open the help of the round function
  • Which animal has the smallest brain to body weight ratio?
    • Hint: have a look at the min function
  • Add the following three animals to the data vectors
species_new <- c("Eagle", "Snail", "Lion")
brainwt_kg_new <- c(0.0004, NA, 0.5)
bodywt_kg_new <- c(18, 0.01, 550)

Now calculate the mean brain weight again. Can you explain what happens? Can you fix it?

  • Hint: have a look at ?mean