Skip to main content

Loops size estimates vectorized (solution)

NoteExercise

The length of an organism is typically strongly correlated with its body mass. This is useful because it allows us to estimate the mass of an organism even if we only know its length. This relationship generally takes the form:

mass = a * lengthb

Parameters a and b vary among groups.

Cut and paste the following vectors into R.

theropoda_lengths <- c(17.8013631070471, 20.3764452071665, 14.0743486294308, 25.65782386974, 26.0952008049675, 20.3111541103134, 17.5663244372533, 11.2563431277577, 20.081903202614, 18.6071626441984, 18.0991894513166, 23.0659685685892, 20.5798853467837, 25.6179254233558, 24.3714331573996, 26.2847248252537, 25.4753783544473, 20.4642089867304, 16.0738256364701, 20.3494171706583, 19.854399305869, 17.7889814608919, 14.8016421998303, 19.6840911485379, 19.4685885050906, 24.4807784966691, 13.3359960054899, 21.5065994598917, 18.4640304608411, 19.5861532398676, 27.084751999756, 18.9609366301798, 22.4829168046521, 11.7325716149514, 18.3758846100456, 15.537504851634, 13.4848751773738, 7.68561192214935, 25.5963348603783, 16.588285389794)

a_values <- c(0.759, 0.751, 0.74, 0.746, 0.759, 0.751, 0.749, 0.751, 0.738, 0.768, 0.736, 0.749, 0.746, 0.744, 0.749, 0.751, 0.744, 0.754, 0.774, 0.751, 0.763, 0.749, 0.741, 0.754, 0.746, 0.755, 0.764, 0.758, 0.76, 0.748, 0.745, 0.756, 0.739, 0.733, 0.757, 0.747, 0.741, 0.752, 0.752, 0.748)

b_values <- c(3.627, 3.633, 3.626, 3.633, 3.627, 3.629, 3.632, 3.628, 3.633, 3.627, 3.621, 3.63, 3.631, 3.632, 3.628, 3.626, 3.639, 3.626, 3.635, 3.629, 3.642, 3.632, 3.633, 3.629, 3.62, 3.619, 3.638, 3.627, 3.621, 3.628, 3.628, 3.635, 3.624, 3.621, 3.621, 3.632, 3.627, 3.624, 3.634, 3.621)
  1. Write a function named mass_from_length_theropoda() that takes dino_length as an argument to get an estimate of mass values for the dinosaur Theropoda. Use the equation mass <- 0.73 * dino_length^3.63. Use the function to estimate masses for all dinosaurs in theropoda_lengths.
  2. Create a data frame for this data named dino_data. Use dplyr to create a new version of this data frame with a masses column (using mutate() and mass_from_length_theropoda()).
  3. Create a new version of the function, call it mass_from_length(), have it use the equation mass <- a * dino_length^b and take dino_length, a and b as arguments. In the function arguments, set the default values for a to 0.73 and b to 3.63. If you run this function with just the length data from Part 1, you should get the same result as Part 1. Call your function using theropoda_lengths, a_values, and b_values to estimate the mass for the dinosaurs using different values of a and b.
  4. Use dplyr to create a new version of dino_data with a masses column created by mass_from_length() with theropoda_lengths, a_values, and b_values as the arguments.
CautionOutput solution

Part 1

 [1]  25262.027  41253.332  10767.568  95233.732 101260.017  40775.516
 [7]  24072.130   4785.145  39129.521  29666.193  26830.297  64700.869
[13]  42768.180  94697.262  79013.471 103955.226  92798.465  41901.983
[19]  17439.569  41055.045  37544.201  25198.303  12928.490  36388.290
[25]  34962.862  80307.929   8854.525  50183.194  28846.165  35735.369
[31] 115908.187  31765.368  58958.713   5561.862  28349.410  15418.314
[37]   9218.648   1197.666  94407.873  19552.500

Part 2


Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
   theropoda_lengths a_values b_values     masses
1          17.801363    0.759    3.627  25262.027
2          20.376445    0.751    3.633  41253.332
3          14.074349    0.740    3.626  10767.568
4          25.657824    0.746    3.633  95233.732
5          26.095201    0.759    3.627 101260.017
6          20.311154    0.751    3.629  40775.516
7          17.566324    0.749    3.632  24072.130
8          11.256343    0.751    3.628   4785.145
9          20.081903    0.738    3.633  39129.521
10         18.607163    0.768    3.627  29666.193
11         18.099189    0.736    3.621  26830.297
12         23.065969    0.749    3.630  64700.869
13         20.579885    0.746    3.631  42768.180
14         25.617925    0.744    3.632  94697.262
15         24.371433    0.749    3.628  79013.471
16         26.284725    0.751    3.626 103955.226
17         25.475378    0.744    3.639  92798.465
18         20.464209    0.754    3.626  41901.983
19         16.073826    0.774    3.635  17439.569
20         20.349417    0.751    3.629  41055.045
21         19.854399    0.763    3.642  37544.201
22         17.788981    0.749    3.632  25198.303
23         14.801642    0.741    3.633  12928.490
24         19.684091    0.754    3.629  36388.290
25         19.468589    0.746    3.620  34962.862
26         24.480778    0.755    3.619  80307.929
27         13.335996    0.764    3.638   8854.525
28         21.506599    0.758    3.627  50183.194
29         18.464030    0.760    3.621  28846.165
30         19.586153    0.748    3.628  35735.369
31         27.084752    0.745    3.628 115908.187
32         18.960937    0.756    3.635  31765.368
33         22.482917    0.739    3.624  58958.713
34         11.732572    0.733    3.621   5561.862
35         18.375885    0.757    3.621  28349.410
36         15.537505    0.747    3.632  15418.314
37         13.484875    0.741    3.627   9218.648
38          7.685612    0.752    3.624   1197.666
39         25.596335    0.752    3.634  94407.873
40         16.588285    0.748    3.621  19552.500

Part 3

 [1]  26039.686  42825.603  10800.224  98273.049 104257.481  41822.386
 [7]  24840.644   4899.022  39915.948  30937.922  26354.908  66384.865
[13]  43837.944  97141.451  80553.856 105556.405  97374.660  42760.136
[19]  18749.274  42109.012  40674.182  26003.425  13229.824  37472.789
[25]  34684.033  80187.272   9460.977  51630.571  29253.772  36399.306
[31] 117511.962  33384.288  58581.226   5462.316  28637.745  15864.172
[37]   9284.810   1218.755  98522.609  19534.524

Part 4

   theropoda_lengths a_values b_values     masses
1          17.801363    0.759    3.627  26039.686
2          20.376445    0.751    3.633  42825.603
3          14.074349    0.740    3.626  10800.224
4          25.657824    0.746    3.633  98273.049
5          26.095201    0.759    3.627 104257.481
6          20.311154    0.751    3.629  41822.386
7          17.566324    0.749    3.632  24840.644
8          11.256343    0.751    3.628   4899.022
9          20.081903    0.738    3.633  39915.948
10         18.607163    0.768    3.627  30937.922
11         18.099189    0.736    3.621  26354.908
12         23.065969    0.749    3.630  66384.865
13         20.579885    0.746    3.631  43837.944
14         25.617925    0.744    3.632  97141.451
15         24.371433    0.749    3.628  80553.856
16         26.284725    0.751    3.626 105556.405
17         25.475378    0.744    3.639  97374.660
18         20.464209    0.754    3.626  42760.136
19         16.073826    0.774    3.635  18749.274
20         20.349417    0.751    3.629  42109.012
21         19.854399    0.763    3.642  40674.182
22         17.788981    0.749    3.632  26003.425
23         14.801642    0.741    3.633  13229.824
24         19.684091    0.754    3.629  37472.789
25         19.468589    0.746    3.620  34684.033
26         24.480778    0.755    3.619  80187.272
27         13.335996    0.764    3.638   9460.977
28         21.506599    0.758    3.627  51630.571
29         18.464030    0.760    3.621  29253.772
30         19.586153    0.748    3.628  36399.306
31         27.084752    0.745    3.628 117511.962
32         18.960937    0.756    3.635  33384.288
33         22.482917    0.739    3.624  58581.226
34         11.732572    0.733    3.621   5462.316
35         18.375885    0.757    3.621  28637.745
36         15.537505    0.747    3.632  15864.172
37         13.484875    0.741    3.627   9284.810
38          7.685612    0.752    3.624   1218.755
39         25.596335    0.752    3.634  98522.609
40         16.588285    0.748    3.621  19534.524