Size Estimates By Name Apply (Loops)
This is a followup to Size Estimates by Name.
Download the data on dinosaur lengths with species names into your data folder and import it using read.csv()
.
Write a function get_mass_from_length_by_name()
that uses the equation mass <- a * length^b
to estimate the size of a dinosaur from its length. This function should take 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 set a
and b
to the appropriate values.
- Stegosauria:
a = 10.95
andb = 2.64
(Seebacher 2001). - Theropoda:
a = 0.73
andb = 3.63
(Seebacher 2001). - Sauropoda:
a = 214.44
andb = 1.46
(Seebacher 2001).
If the name is not any of these values set a = NA
and b = 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()
).