Name:
Andrew ID:

Instructions

Warming up, basic data manipulations (15 points)

Debugging and merging (15 points)

# Function: medals.comaprison, to compare the medal count from one country to 
#   the count from all others
# Inputs:
# - df: data frame (assumed to have the same column structure as the rio data 
#   frame)
# - country: string, the country to be examined (e.g., "USA")
# - medal: string, the medal type to be examined (e.g., "gold")
# Output: numeric vector of length 2, giving the medal count from the given
#   country and all other countries
medals.comparison = function(df, country, medal) {
  ind = which(rio$nationality == country)
  country.df = df[ind,]
  others = df[!ind,]
  country.sum = sum(country.df$medal)
  others.sum = sum(others$medal)
  return(country.sum, others.sum)
}

#all.equal(medals.comparison(rio, "USA", "gold"), c(139, 527))
#all.equal(medals.comparison(rio, "USA", "silver"), c(54, 601))
#all.equal(medals.comparison(rio, "USA", "bronze"), c(71, 633))
#all.equal(medals.comparison(rio[rio$sport=="rowing",], "CAN", "silver"), 
#          c(2, 46))
# Function: date.converter, to convert a dates of the form DD.MM.YY to 
#   YYYY-MM-DD (assuming the year is before 2000)
# Inputs:
# - date: factor of dates of the form DD.MM.YY 
# Output: string vector of dates of the form YYYY-MM-DD
date.converter = function(date) {
  date = as.character(date)
  date.split = strsplit(date, ".")
  date.new = lapply(date.split, function(x) {
    paste("19", x[3], "-", x[2], "-", x[1])
  })
  return(date.new)
}

#all.equal(date.converter("15.12.85"), "1985-12-15")
#all.equal(date.converter(c("08.04.82", "08.07.84")), 
#          c("1982-04-08", "1984-07-08"))

Simulation, iteration, and regression (14 points)

Text processing and functions (32 points)

#load(url("http://www.stat.cmu.edu/~ryantibs/statcomp-S18/data/wiki.rdata"))