--- title: | | STAT 408 - Week 5: | R Miscellanea and Debugging R Code date: "February 8, 2018" output: html_document --- ```{r setup, include=FALSE} library(knitr) library(formatR) library(XML) library(dplyr) knitr::opts_chunk$set(echo = TRUE) knitr::knit_hooks$set(mysize = function(before, options, envir) { if (before) return(options$size) }) ``` ## Exercise: Lists Consider the two lists, write out what gets printed from R. ```{r, eval=F, mysize=TRUE, size='\\tiny'} msu.info <- list( name = c('Waded Cruzado','Andy Hoegh'), degree.from = c('University of Texas at Arlington','Virginia Tech'), job.title = c('President', 'Assistant Professor of Statistics')) msu.info msu.info2 <- list(c('Waded Cruzado','University of Texas at Arlington', 'President'), c('Andy Hoegh', 'Virginia Tech','Assistant Professor of Statistics')) msu.info2 ``` \normalsize What do all of those brackets mean? ## Exercise: Lists Explore the indexing with these commands. ```{r, mysize=TRUE, size='\\tiny', eval=F} msu.info <- list( name = c('Waded Cruzado','Andy Hoegh'), degree.from = c('University of Texas at Arlington','Virginia Tech'), job.title = c('President', 'Assistant Professor of Statistics')) msu.info[1] msu.info[[1]] msu.info$name[2] msu.info[1:2] unlist(msu.info) ``` ## Exercise: Arrays Create an array of dimension 2 x 2 x 3, where each of the three 2 x 2 subarray (or matrix) is the Identity matrix. ## Merge Another important skill is merging or combining data sets. Consider the two data frames, how can we merge them and what should be the dimensions of the merged data frame. ```{r, mysize=TRUE, size='\\scriptsize'} df1 <- data.frame(school = c('MSU','VT','Mines'), state= c('MT','VA','CO'), stringsAsFactors = F) df1 df2 <- data.frame(school = c('Mines','MSU','VT'), enrollment = c(5794,15688,30598), stringsAsFactors = F) df2 ``` ## Exercise: merging Combine the two data sets ```{r, mysize=TRUE, size='\\tiny'} df.cost <- data.frame( ski.resort = c('Bridger Bowl', 'Big Sky', 'Steamboat', 'Jackson'), ticket.cost = c(60, 'depends',145, 130)) df.acres <- data.frame( ski.hill = c('Bridger Bowl', 'Jackson', 'Steamboat', 'Big Sky'), skiable.acres = c(2000, "2500+",2965, 5800)) ``` # Debugging R code ## Exercise: Debugging a Warning Fix the script that determines if each item in a sequence is less than zero. ```{r, mysize=TRUE, size='\\tiny'} val.in <- seq(-1,1,by=.25) if (val.in < 0){ print(paste(val.in, 'less than 0')) } ``` ## Exercise: Debugging an Error Identify the issue(s) with this function ```{r, error=TRUE, eval=FALSE, mysize=TRUE, size='\\footnotesize'} MergeData <- function(data1, data2, key1, key2){ # function to merge two data sets # Args: data1 - first dataset # data2 - second dataset # key1 - key name in first dataset # key2 - key name in second dataset # Returns: merged dataframe if key matches, # otherwise print an error if (key1 = key2){ data.out <- join(data1,data2, by = key1) return(dataout) } else { stop('keys are not the same') } } MergeData(df1,df2,"school","school") ```