for Loop (Functions)
This is a follow up to Nested Functions.
-
Now that you’ve impressed Grandma, it’s time to do some serious science. Take the following vector of Stegosaur lengths
lengths <- c(10.1, 9.5, 11.2, 9.8, 10.4, 12.0, 11.5, 9.5, 9.8, 10.0, 10.7, 10.2, 11.9, 9.7, 11.2, 11.8, 10.7)
and estimate the mass in kilograms for each length using a
for
loop, your function for estimating mass,a = 10.95
, andb = 2.64
. Print the results in order. -
This is a good way to learn how to use a
for
loop, but thanks to vectorization in R we can also just pass the entirelengths
vector to our function. Use this approach to estimate the mass for each length and display the output.