Size Estimates By Name Apply (Loops)
If the data on dinosaur lengths with species names is not in your working directory then download it. Import it using read_csv().
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)
}
-
Copy this function into your code and then use this function and
map2_dbl()to calculate the estimated mass for each dinosaur. You’ll need to pass the data tomap2_dbl()as single vectors or columns, not the whole data frame. -
Using
dplyr, add a newmassescolumn to the data frame (usingrowwise(),mutate()and your function) and print the result to the console. -
Using
ggplot, make a histogram of dinosaur masses with one subplot for each species (usingfacet_wrap()).