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()
.
You’re going to write a function to estimate a dinosaur’s mass based on its length and name of its taxonomic group. The general form of the equation for doing this is:
mass <- a * length ^ b
The parameters a
and b
vary by the group of dinosaurs, so you
decide to create a function that lets you specify which dinosaur group you need
to estimate the size of by name and then have the function automatically choose
the right parameters.
Create a function get_mass_from_length_by_name()
that takes two arguments,
the length
and the name of the dinosaur group. Inside this function use
if
/else if
/else
statements to check to see if the name is one of the
following values and if so use the associated a
and b
values to estimate the
species mass using these equations:
- Stegosauria:
mass = 10.95 * length ^ 2.64
(Seebacher 2001) - Theropoda:
mass = 0.73 * length ^ 3.63
(Seebacher 2001) - Sauropoda:
mass = 214.44 * length ^ 1.46
(Seebacher 2001)
If the name is not any of these values the function should return NA
.
-
Use this function and
mapply()
to calculate the estimated mass for each dinosaur. You’ll need to pass the data tomapply()
as single vectors or columns, not the whole data frame. -
Using
dplyr
, add a newmasses
column 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()
).