R Markdown

Problem 2: Working with vectors and matrices

#Box office Star Wars (in millions)

new_hope <- c(460.998,314.4)
empire_strikes <- c(290.475,247.900)
return_jedi <- c(309.306,165.8)

#Vectors region and titles, used for naming

region <- c("US","non-US")
titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")

#2A. Construct a matrix where rows represent each movie. Name this matrix starWars.

starWars <- matrix(data= c(460.998,314.4,290.475,247.900,309.306,165.8),nrow = 3,ncol = 2,byrow = TRUE)
starWars
##         [,1]  [,2]
## [1,] 460.998 314.4
## [2,] 290.475 247.9
## [3,] 309.306 165.8

#2B. Rename rows and columns of matrix

rownames(starWars) <- titles
starWars
##                            [,1]  [,2]
## A New Hope              460.998 314.4
## The Empire Strikes Back 290.475 247.9
## Return of the Jedi      309.306 165.8
colnames(starWars) <- region
starWars
##                              US non-US
## A New Hope              460.998  314.4
## The Empire Strikes Back 290.475  247.9
## Return of the Jedi      309.306  165.8

#2C. Calculate the worldwide box office figures for each movie using rowSums(). Name and output this vector.

worldwide <-rowSums(starWars)
worldwide
##              A New Hope The Empire Strikes Back      Return of the Jedi 
##                 775.398                 538.375                 475.106

#2D. Add row sums to starWars matrix

starWars <- cbind(starWars, worldwide)
starWars
##                              US non-US worldwide
## A New Hope              460.998  314.4   775.398
## The Empire Strikes Back 290.475  247.9   538.375
## Return of the Jedi      309.306  165.8   475.106

#2E. Create another matrix for the prequels and name it starWars2.

phantom_menace <- c(474.5,552.5)
attack_clones <- c(310.7,338.7)
revenge_sith <- c(380.3,468.5)
titles2 <- c("The Phantom Menance", "Attack of the Clones", "Revenge of the Sith")
starWars2<- rbind(phantom_menace,attack_clones,revenge_sith)
starWars2
##                 [,1]  [,2]
## phantom_menace 474.5 552.5
## attack_clones  310.7 338.7
## revenge_sith   380.3 468.5
colnames(starWars2) <-region
rownames(starWars2) <-titles2
starWars2
##                         US non-US
## The Phantom Menance  474.5  552.5
## Attack of the Clones 310.7  338.7
## Revenge of the Sith  380.3  468.5
worldwide2 <- rowSums(starWars2)
starWars2 <- cbind(starWars2,worldwide2)
starWars2
##                         US non-US worldwide2
## The Phantom Menance  474.5  552.5     1027.0
## Attack of the Clones 310.7  338.7      649.4
## Revenge of the Sith  380.3  468.5      848.8

#2F. Make one big matrix that combines all movies from starWars and starWars2 using rbind().

allStarWars <- rbind(starWars,starWars2)
allStarWars
##                              US non-US worldwide
## A New Hope              460.998  314.4   775.398
## The Empire Strikes Back 290.475  247.9   538.375
## Return of the Jedi      309.306  165.8   475.106
## The Phantom Menance     474.500  552.5  1027.000
## Attack of the Clones    310.700  338.7   649.400
## Revenge of the Sith     380.300  468.5   848.800

#2G. Find the total non-US revenue for all the movies using the colSums() functions.

colSums(allStarWars)
##        US    non-US worldwide 
##  2226.279  2087.800  4314.079

#Total non-US revenue for all Star Wars movies is 2087.00 million dollars.