Basic for loops

for (item in list_of_items) {
  commands
}

Example

pets <- c("spot", "gigantor", "fluffy")
for (pet in pets) {
  paste(pet, "is the name of the class pet")
}

What this does:

pets <- c("spot", "gigantor", "fluffy")
pet <- pets[1]
paste(pet, "is the name of the class pet")
pet <- pets[2]
paste(pet, "is the name of the class pet")
pet <- pets[3]
paste(pet, "is the name of the class pet")

Storing results

library(stringr)

pets <- c("spot", "gigantor", "fluffy")
output <- data.frame()
for (pet in pets) {
  pet_upper <- str_to_upper(pet)
  output <- rbind(output, data.frame(name = pet_upper,
                          namelength = str_length(pet_upper)))
}

Looping over columns in data frames

biomass_data <- data.frame(exper1 = c(24, 32, 62),
                           exper2 = c(10, 9 , 5),
                           exper3 = c(1, 5, 3))

for (exp_biomass in biomass_data) {
  npp <- sum(19.3 * exp_biomass ** 2)
  print(npp)
}

Complete through exercise 4

Apply

get_mass_from_length_theropoda <- function(length) {
  mass <- 0.73 * length ** 3.63
  return(mass)
}

lengths = c(5, 10, 15)
lapply(lengths, FUN = get_mass_from_length_theropoda)
sapply(lengths, FUN = get_mass_from_length_theropoda)
get_mass_from_length(lengths, as, bs)

MAKE SURE BASICS MAKE SENSE BEFORE COVERING THIS

Dplyr

estimate_mass <- function(hindfoot_length, weight){
  mass 
}
surveys <- read.csv("surveys.csv")

Indexing

pets <- c("spot", "gigantor", "fluffy")
owners <- c("betty", "bob", "joe")
for (i in seq_along(pets)){
  print(paste(pets[i], "is ", owners[i], "'s pet"))
}

Preallocating memory

library(stringr)

pets <- c("spot", "gigantor", "fluffy")
output <- data.frame(name = character(3), namelength = numeric(3),
                     stringsAsFactors = FALSE)
for (i in seq_along(pets)) {
  pet_upper <- str_to_upper(pets[i])
  output[i,] <- c(pet_upper, str_length(pet_upper))
}