1. Use classify_by_weight and a for loop to determine the weight class for each value in the following vector, store the weight classes in a vector, and after the loop display the vector.
weights <-c(15, 25, 35, 45, NA, 70, 72)
2. Use classify_by_weight and a for loop to determine the weight class for each individual in surveys and store the weight classes in a vector. Add this vector to surveys as a new column named weight_class and then use dplyr to count the number of individuals in each weight class.
3. The following function estimates daily energy needs based on an individual’s genus and weight:
energy_budget <-function(genus, weight) {if (genus =="Dipodomys") { energy <-0.065* weight ^0.75*24 } elseif (genus =="Chaetodipus") { energy <-0.080* weight ^0.75*24 } else { energy <-0.073* weight ^0.75*24 }return(energy)}
Use energy_budget and a for loop to estimate the energy budget for each pair of values in the following vectors, store the energy budgets in a vector, and after the loop display the vector.
genera <-c("Dipodomys", "Chaetodipus", "Neotoma", "Dipodomys")weights <-c(45, 22, 156, 38)
CautionOutput solution
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
Rows: 35549 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): species_id, sex
dbl (7): record_id, month, day, year, plot_id, hindfoot_length, weight
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.