Chapter 10 Data Reshaping

Lander's chapter 12 Data Reshaping

###############################
#Chapter12 Data reshaping######
###############################

#cbind and rbind
sport <- c("Hockey", "Baseball", "Football")
league <- c("NHL", "MLB", "NFL")
trophy <- c("Stanley Cup", "Commissioner's Trophy", "Vince Lombardi Trophy")
trophies1 <- cbind(sport, league, trophy)
trophies1
##      sport      league trophy                 
## [1,] "Hockey"   "NHL"  "Stanley Cup"          
## [2,] "Baseball" "MLB"  "Commissioner's Trophy"
## [3,] "Football" "NFL"  "Vince Lombardi Trophy"
trophies2 <- data.frame(sport=c("Basketball", "Golf"),
                        league=c("NBA", "PGA"),
                        trophy=c("Larry O'Brien Championship Trophy", "Wanamaker Trophy"),
                        stringsAsFactors = FALSE)
dim(trophies1)
## [1] 3 3
dim(trophies2)
## [1] 2 3
trophies <- rbind(trophies1, trophies2)

#assign new column names to vectors in cbind
cbind(Sport=sport, Association=league, Prize=trophy)
##      Sport      Association Prize                  
## [1,] "Hockey"   "NHL"       "Stanley Cup"          
## [2,] "Baseball" "MLB"       "Commissioner's Trophy"
## [3,] "Football" "NFL"       "Vince Lombardi Trophy"