Exercise: Lists

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

'President'), c('Andy Hoegh',
'Virginia Tech','Assistant Professor of Statistics'))
msu.info2

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.

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.

df1 <- data.frame(school = c('MSU','VT','Mines'),
state= c('MT','VA','CO'), stringsAsFactors = F)
df1
##   school state
## 1    MSU    MT
## 2     VT    VA
## 3  Mines    CO
df2 <- data.frame(school = c('Mines','MSU','VT'),
enrollment = c(5794,15688,30598), stringsAsFactors = F)
df2
##   school enrollment
## 1  Mines       5794
## 2    MSU      15688
## 3     VT      30598

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

Debugging R code

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')
}
}
MergeData(df1,df2,"school","school")