r_matrix Flashcards
Construct a matrix with 3 rows that contain the numbers 1 up to 9
matrix(1:9, byrow = TRUE, nrow = 3)
Create a matrix with the following characteristics:
- Create matrix
- 3 rows
- byrow = True
- Insert the following vectors:
- new_hope
- empire_strikes
- return_jedi
- Name the columns with the region vector
- Name the rows with the titles vector
- star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow=3, byrow = TRUE)
- colnames(star_wars_matrix) <- region
- rownames(star_wars_matrix) <- titles
Calculate the worldwide box office figures for each star wars movie in the star_wars_matrix matrix, and place in the vector worldwide_vector
worldwide_vector <- rowSums(star_wars_matrix)
Bind the new variable worldwide_vector as a column to star_wars_matrix as a new matrix named all_wars__matrix
all_wars_matrix <- cbind(star_wars_matrix, worldwide_vector)
Use rbind() to paste together star_wars_matrix and star_wars_matrix2, in this order. Assign the resulting matrix to all_wars_matrix.
Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix, star_wars_matrix2)
Calculate the total revenue for the US (column 1) and the non-US (column 2) region and assign total_revenue_vector.
total_revenue_vector <- colSums(all_wars_matrix)
- Select the non-US revenue for all movies (the entire second column of all_wars_matrix), store the result as non_us_all.
- Calculate the average non-US revenue for all movies
- Select the non-US revenue for the first two movies in all_wars_matrix, store in non_us_all
- Use mean() again to print out the average of the values in non_us_some.
- # 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)
You are given the following:
- all_wars _matrix (total sales in millions)
- ticket_prices_matrix (ticket price for each movie)
Do the following:
- get the estimated number of US and non-US visitors for the six movies. Assign the result to visitors.
- From the visitors matrix, store the entire first column, representing the number of visitors in the US. Store this selection as us_visitors
- Calculate the average number of US visitors
- # 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)