Size Estimates By Name Loop (Loops)
If dinosaur_lengths.csv is not already in your working directory download a copy of the data on dinosaur lengths with species names. Load it into R.
The following function estimates a dinosaur’s mass based on its length and name of its taxonomic group:
get_mass_from_length_by_name <- function(length, name){
if (name == "Stegosauria"){
mass = 10.95 * length ^ 2.64
}
else if (name == "Theropoda"){
mass = 0.73 * length ^ 3.63
}
else if (name == "Sauropoda"){
mass = 214.44 * length ^ 1.46
}
else {
mass = NA
}
return(mass)
}
Update this function so that instead of returning NA when none of the species names matches it returns mass = 25.37 * length ^ 2.49 instead.
- Use this function and a for loop to calculate the estimated mass for each dinosaur in
dinosaur_lengths, store the masses in a vector, and after all of the calculations are complete show the first few items in the vector usinghead(). - Add the results in the vector back to the original data frame and display first few rows of the new data frame using
head(). - Calculate the mean mass for each
speciesusingdplyr, using the data from you created in (2).