--3-Matrices
#1-What's a matrix?
#Construct a matrix with 3 rows containing the numbers 1 up to 9,
# filled row-wise.
matrix(1:9,byrow=TRUE,nrow=3)
#2-Analyzing matrices
#Use c(new_hope, empire_strikes, return_jedi) to combine-
#the three vectors into one vector. Call this vector box_office.
#Construct a matrix with 3 rows, where each row represents a movie.
#Use the matrix() function to this.
#The first argument is the vector box_office, containing
#all box office figures.
#Next, you'll have to specify nrow = 3 and byrow = TRUE.
#Name the resulting matrix star_wars_matrix.
# 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)
# Create box_office
box_office <-c(new_hope,empire_strikes,return_jedi)
# Construct star_wars_matrix
star_wars_matrix <- matrix(box_office,byrow=TRUE,nrow=3)
star_wars_matrix
#3-Naming a matrix
#Use colnames() to name the columns of star_wars_matrix with
#the region vector.
#Use rownames() to name the rows of star_wars_matrix with the
#titles vector.
#Print out star_wars_matrix to see the result of your work.
# 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)
# Construct matrix
star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi),
nrow = 3, byrow = TRUE)
# 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")
# Name the columns with region
colnames(star_wars_matrix)<-region
# Name the rows with titles
rownames(star_wars_matrix)<-titles
# Print out star_wars_matrix
star_wars_matrix
#4-Calculating the worldwide box office
#Calculate the worldwide box office figures for
#the three movies and put these in the vector named worldwide_vector.
# Construct star_wars_matrix
box_office <-c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi"),
c("US", "non-US")))
star_wars_matrix
# Calculate worldwide box office figures
worldwide_vector <- rowSums(star_wars_matrix)
worldwide_vector
#5-Adding a column for the Worldwide box office
#Add worldwide_vector as a new column to the star_wars_matrix and
#assign the result to all_wars_matrix.
#Use the cbind() function.
# Construct star_wars_matrix
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi"),
c("US", "non-US")))
star_wars_matrix
# The worldwide box office figures
worldwide_vector <- rowSums(star_wars_matrix)
worldwide_vector
# Bind the new variable worldwide_vector as a column to
#star_wars_matrix
all_wars_matrix <- cbind(star_wars_matrix,worldwide_vector)
all_wars_matrix
#6-Adding a row
#Use rbind() to paste together star_wars_matrix and star_wars_matrix2,
#in this order.
#Assign the resulting matrix to all_wars_matrix.
# Construct star_wars_matrix1
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi")
,
c("US", "non-US")))
star_wars_matrix
# Construct star_wars_matrix2
box_office2 <- c( 474.5, 552.5, 552.5, 338.7, 380.3, 468.5)
star_wars_matrix2 <- matrix(box_office2, nrow = 3, byrow = TRUE,
dimnames = list(c("The Phantom Menace", "Attack of the Clones",
"Revenge of the Sith")
, c("US", "non-US")))
star_wars_matrix2
# star_wars_matrix and star_wars_matrix2 are available in
#your workspace
star_wars_matrix
star_wars_matrix2
# Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix,star_wars_matrix2)
all_wars_matrix
#7-The total box office revenue for the entire saga
# Construct star_wars_matrix1
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi")
,
c("US", "non-US")))
star_wars_matrix
# Construct star_wars_matrix2
box_office2 <- c( 474.5, 552.5, 552.5, 338.7, 380.3, 468.5)
star_wars_matrix2 <- matrix(box_office2, nrow = 3, byrow = TRUE,
dimnames = list(c("The Phantom Menace", "Attack of the Clones",
"Revenge of the Sith")
,
c("US", "non-US")))
star_wars_matrix2
# star_wars_matrix and star_wars_matrix2 are available in
#your workspace
star_wars_matrix
star_wars_matrix2
# Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix,star_wars_matrix2)
all_wars_matrix
# Total revenue for US and non-US
total_revenue_vector <- colSums(all_wars_matrix)
# Print out total_revenue_vector
total_revenue_vector
#8-Selection of matrix elements
# Construct star_wars_matrix1
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi")
,
c("US", "non-US")))
star_wars_matrix
# Construct star_wars_matrix2
box_office2 <- c( 474.5, 552.5, 552.5, 338.7, 380.3, 468.5)
star_wars_matrix2 <- matrix(box_office2, nrow = 3, byrow = TRUE,
dimnames = list(c("The Phantom Menace", "Attack of the Clones",
"Revenge of the Sith")
,
c("US", "non-US")))
star_wars_matrix2
# star_wars_matrix and star_wars_matrix2 are available in
#your workspace
star_wars_matrix
star_wars_matrix2
# Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix,star_wars_matrix2)
all_wars_matrix
# Select the non-US revenue for all movies
non_us_all <- all_wars_matrix[,2]
# Average non-US revenue
mean(non_us_all)
# Select the non-US revenue for first two movies
non_us_some <- all_wars_matrix[1:2,2]
# Average non-US revenue for first two movies
mean(non_us_some)
#9-A little arithmetic with matrices
# Construct star_wars_matrix1
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi")
,
c("US", "non-US")))
star_wars_matrix
# Construct star_wars_matrix2
box_office2 <- c( 474.5, 552.5, 552.5, 338.7, 380.3, 468.5)
star_wars_matrix2 <- matrix(box_office2, nrow = 3, byrow = TRUE,
dimnames = list(c("The Phantom Menace", "Attack of the Clones",
"Revenge of the Sith")
,
c("US", "non-US")))
star_wars_matrix2
# star_wars_matrix and star_wars_matrix2 are available in your workspace
star_wars_matrix
star_wars_matrix2
# Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix,star_wars_matrix2)
all_wars_matrix
# Estimate the visitors
visitors <- all_wars_matrix/5
# Print the estimate to the console
visitors
#10- A little arithmetic with matrices (2)
# Construct star_wars_matrix1
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi")
,
c("US", "non-US")))
star_wars_matrix
# Construct star_wars_matrix2
box_office2 <- c( 474.5, 552.5, 552.5, 338.7, 380.3, 468.5)
star_wars_matrix2 <- matrix(box_office2, nrow = 3, byrow = TRUE,
dimnames = list(c("The Phantom Menace", "Attack of the Clones",
"Revenge of the Sith")
,
c("US", "non-US")))
star_wars_matrix2
# star_wars_matrix and star_wars_matrix2 are available in
#your workspace
star_wars_matrix
star_wars_matrix2
# Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix,star_wars_matrix2)
all_wars_matrix
# create ticket_prices_matrix
box_office <- c(5,5, 6,6, 7,7)
ticket_prices_matrix1 <- matrix(box_office, nrow = 3, byrow = TRUE,
dimnames = list(c("A New Hope", "The Empire Strikes Back",
"Return of the Jedi"),
c("US", "non-US")))
# Construct ticket_prices_matrix2
box_office2 <- c(4,4, 4.5,4.5, 4.9,4.9)
ticket_prices_matrix2<- matrix(box_office2, nrow = 3, byrow = TRUE,
dimnames = list(c("The Phantom Menace", "Attack of the Clones",
"Revenge of the Sith")
,
c("US", "non-US")))
ticket_prices_matrix<-rbind(ticket_prices_matrix1,ticket_prices_matrix2)
# Estimated number of visitors
visitors <- all_wars_matrix/ticket_prices_matrix
# US visitors
us_visitors <- visitors[,1]
# Average number of US visitors
mean(us_visitors)
Last updated