Set up R console:

library(stringr)
library(dplyr)

Basic for loop

for (item in list_of_items) {
  do_something(item)
}
pets <- c("spot", "gigantor", "fluffy")
for (pet in pets) {
  print(pet)
}
pets <- c("spot", "gigantor", "fluffy")
pet <- pets[1]
print(pet)
pet <- pets[2]
print(pet)
pet <- pets[3]
print(pet)
pets <- c("spot", "gigantor", "fluffy")
for (pet in pets) {
  pet_upper <- str_to_upper(pet)
  print(pet_upper)
}

Do Exercise 1.1 - for Loop.

Make sure students get the basics before moving on.

Storing results

pets <- c("spot", "gigantor", "fluffy")
upper_case_pets <- c()
for (pet in pets){
  pet_upper <- str_to_upper(pet)
  upper_case_pets <- c(upper_case_pets, pet_upper)
  print(upper_case_pets)
}

Looping in data frames

pets <- data.frame(pet_name = c("spot", "gigantor", "fluffy"),
                   pet_type = c("fish", "hamster", "lizard"))
for (pet in pets){
  print("Start new loop")
  print(pet)
}
pets <- data.frame(pet_name = c("spot", "gigantor", "fluffy"),
                   pet_type = c("fish", "hamster", "lizard"))
for (i in 1:nrow(pets)){
  print(paste(pets$pet_name[i], " is a ", pets$pet_type[i], sep =""))
}
output <- data.frame(name = character(3), namelength = numeric(3),
                     stringsAsFactors = FALSE)
for (i in 1:nrow(pets)) {
  pet_upper <- str_to_upper(pets$pet_name[i])
  pet_length <- str_length(pets$pet_type[i])
  output[i,] <- c(pet_upper, pet_length)
}

Assign Exercise 2 - stringr. Assign Exercise 3 - DNA or RNA.

Alternatives to loops

Vectorization

lengths <- c(2.26, 1.48, 3.84)
mass <- 0.73 * length ** 3.63
mass

Looping over files

date,time,lat,long
2016-01-01,4:20,26.16,-35.28
download.file("http://www.datacarpentry.org/semester-biology/data/collar-data-2016-01.zip")
unzip("collar-data-2016-01.zip")
list.files()
collar_data_files <- list.files("collar-data-2016-01", 
                                pattern="collar-data-.*.txt",
                                full.names=TRUE)
num_samps <- c()
for (data_file in collar_data_files){
  data <- read.csv(data_file)
  samples <- nrow(data)
  num_samps <- c(num_samps, samples) 
}
num_samps
get_num_samps <- function(data_file_name){
  data <- read.csv(data_file_name)
  samples <- nrow(data)
  return(samples)
}

num_samps <- c()
for (data_file in collar_data_files){
  num_samps <- c(num_samps, get_num_samps(data_file))
}
num_samps

apply/map

num_samps = sapply(collar_data_files, get_num_samps)

dplyr

num_samps <- data.frame(myfiles, stringsAsFactors=FALSE) %>%
  rowwise() %>%
  mutate(samples = get_num_samps(myfiles))
num_samps <- data.frame(myfiles) %>%
  rowwise() %>%
  mutate(samples = get_num_samps(myfiles))
num_samps <- data.frame(myfiles) %>%
  rowwise() %>%
  mutate(samples = typeof(myfiles))