Functions

for Loop


This is a follow up to Nested Functions.

  1. 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, and b = 2.64. Print the results in order.

  2. 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 entire lengths vector to our function. Use this approach to estimate the mass for each length and display the output.

[click here for output] [click here for output]