Exercise: Lists

Consider the two lists, write out what gets printed from R.

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

Additionally what do all of those brackets mean?

Exercise: Lists

Explore the indexing with these commands.

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.

Exercise: merging

Combine the two data sets

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))

Exercise: Debugging a Warning

Fix the script that determines if each item in a sequence is less than zero.

val.in <- seq(-1,1,by=.25)

if (val.in < 0){
  print(paste(val.in, 'less than 0'))
}
## Warning in if (val.in < 0) {: the condition has length > 1 and only the
## first element will be used
## [1] "-1 less than 0"    "-0.75 less than 0" "-0.5 less than 0" 
## [4] "-0.25 less than 0" "0 less than 0"     "0.25 less than 0" 
## [7] "0.5 less than 0"   "0.75 less than 0"  "1 less than 0"

Exercise: Debugging an Error

Identify the issue(s) with this function

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')
  }
}