Apply functions

sapply

est_mass <- function(volume){
  mass <- 2.65 * volume^0.9
  return(mass)
}

shrub_vol1 <- 1.6
est_mass(shrub_vol1)
shrub_vol2 <- 5.6
est_mass(shrub_vol2)

shrub_vol3 <- 3.1
est_mass(shrub_vol3)
shrub_vols <- c(1.6, 5.6, 3.1)
sapply(shrub_vols, est_mass)

Do Task 1 of Use and Modify with Apply.

Other apply functions

lapply(shrub_vols, est_mass)
est_mass_type <- function(volume, veg_type){
  if (veg_type == "tree"){
    mass <- 2.65 * volume^0.9
  } else {
    mass <- NA
  }
  return(mass)
}

est_mass_type(1.6, "tree")
plant_vols <- c(1.6, 3, 8)
plant_types <- c("tree", "grass", "tree")
mapply(est_mass_type, volume = plant_vols, veg_type = plant_types)

Do Task 2 of Use and Modify with Apply.

tidyverse version of apply

library(purrr)
map(plant_vols, est_mass)
library(dplyr)
plant_vols_df = data.frame(vols = plant_vols)
plant_vols_df %>% 
  filter(vols > 2) %>% 
  map(est_mass)

Do Crown Volume Calculation.

For loops

Set up R console:

library(stringr)
library(dplyr)

Basic for loop

waterbirds <- c("cygnus olor", "aix sponsa", "anas acuta")
waterbird <- waterbirds[1]
print(waterbird)
waterbird <- waterbirds[2]
print(waterbird)
waterbird <- waterbirds[3]
print(waterbird)
for (item in list_of_items) {
  do_something(item)
}
for (waterbird in waterbirds){
  print(waterbird)
}
for (waterbird in waterbirds){
  waterbird_cap <- str_to_title(waterbird)
  print(waterbird_cap)
}

Do Basic Vector.

Numeric values in for loops

for (num in 100:150){
  print(num * 10)
}
for (num in 100:150){
  print(paste("My favorite number is", num * 10))
}

Do Basic Index tasks 1-2.

Storing results

output <- c()
output <- c(1, 2, 3)
output <- c(output, 4)
waterbirds_cap_list <- c()
for (waterbird in waterbirds){
  waterbird_cap <- str_to_title(waterbird)
  waterbirds_cap_list <- c(waterbirds_cap_list, waterbird_cap)
  print(waterbirds_cap_list)
}
waterbirds_cap_list

Do Basic Index task 3.

Looping in data frames

waterbirds <- data.frame(sci_name = c("cygnus olor", 
                                      "aix sponsa", 
                                      "anas acuta"), 
                         common_name = c("mute swan", 
                                         "wood duck", 
                                         "pintail"))
for (waterbird in waterbirds){
  print("Start new loop")
  print(waterbird)
}
for (i in 1:nrow(waterbirds)){
  print(i)
}
for (r in 1:nrow(waterbirds)){
  print(r)
}
for (i in 1:nrow(waterbirds)){
  print(waterbirds$sci_name[i])
}
for (i in 1:nrow(waterbirds)){
  print(paste(waterbirds$sci_name[i], "is a", 
              waterbirds$common_name[i]))
}
waterbirds_2 <- data.frame(capital_name = character(3), 
                           name_length = numeric(3), 
                           stringsAsFactors = FALSE)
for (i in 1:nrow(waterbirds)){
  common_name_cap <- str_to_title(waterbirds$common_name[i])
  sci_name_length <- str_length(waterbirds$sci_name[i])
  waterbirds_2[i,] <- c(common_name_cap, sci_name_length)
}

Do stringr.

Looping over files

download.file("http://www.datacarpentry.org/semester-biology/data/collar-data-2016-01.zip", 
              "collar_data.zip")
unzip("collar_data.zip")
collar_data_files = list.files(pattern = "collar-data-.*.txt", 
                               full.names = TRUE)
  1. With loop

     numbers_vector_1 <- c()
     for (data_file in collar_data_files){
       file <- read.csv(data_file)
       number <- nrow(file)
       numbers_vector_1 <- c(numbers_vector_1, number)
     }
    
  2. With function and loop

     get_numbers <- function(data_file_name){
       file <- read.csv(data_file_name)
       number <- nrow(file)
       return(number)
     }
    	
     numbers_vector_2 <- c()
     for (data_file in collar_data_files){
       numbers_vector_2 <- c(numbers_vector_2, get_numbers(data_file))
     }
    
  3. With function and apply

numbers_vector_3 <- unlist(lapply(collar_data_files, get_numbers))

Do Multiple Files.

Do Species Occurrences Elevation Histogram.