Problem 2

Construct a matrix, where rows represent each movie. Name this matrix starWars and output it.

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

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

Rename the rows and columns of the matrix you created in Part A with the vector region for columns and the vector titles for rows. Then print the matrix

colnames(starWars) <- region
row.names(starWars) <- titles
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

Calculate the worldwide box office figures for each movie usinng the rowSums() function. Name and ouput this vector.

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

Now we want to add a column to our matrix for worldwide sales.

starWars <- cbind(starWars, world)
starWars
##                              US non-US   world
## 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

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 Menace", "Attack of the Clones", "Revenge of the Sith")

starWars2 <- rbind(phantom_menace, attack_clones, revenge_sith)

colnames(starWars2) <- region
row.names(starWars2) <- titles2
world <- rowSums(starWars2)
starWars2 <- cbind(starWars2, world)
starWars2
##                         US non-US  world
## The Phantom Menace   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

Make one big matrix that combines the movies.

allStarWars <- rbind(starWars, starWars2)
allStarWars
##                              US non-US    world
## 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 Menace      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

Find the total non-US revenue for all the movies using the colSums() function.

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

The total non-US revenue for all movies is $2087.800 (in millions).