r_matrix Flashcards

1
Q

Construct a matrix with 3 rows that contain the numbers 1 up to 9

A

matrix(1:9, byrow = TRUE, nrow = 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a matrix with the following characteristics:

  1. Create matrix
    • 3 rows
    • byrow = True
    • Insert the following vectors:
      • new_hope
      • empire_strikes
      • return_jedi
  2. Name the columns with the region vector
  3. Name the rows with the titles vector
A
  1. star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow=3, byrow = TRUE)
  2. colnames(star_wars_matrix) <- region
  3. rownames(star_wars_matrix) <- titles
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Calculate the worldwide box office figures for each star wars movie in the star_wars_matrix matrix, and place in the vector worldwide_vector

A

worldwide_vector <- rowSums(star_wars_matrix)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Bind the new variable worldwide_vector as a column to star_wars_matrix as a new matrix named all_wars__matrix

A

all_wars_matrix <- cbind(star_wars_matrix, worldwide_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use rbind() to paste together star_wars_matrix and star_wars_matrix2, in this order. Assign the resulting matrix to all_wars_matrix.

A

Combine both Star Wars trilogies in one matrix
all_wars_matrix <- rbind(star_wars_matrix, star_wars_matrix2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Calculate the total revenue for the US (column 1) and the non-US (column 2) region and assign total_revenue_vector.

A

total_revenue_vector <- colSums(all_wars_matrix)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Select the non-US revenue for all movies (the entire second column of all_wars_matrix), store the result as non_us_all.
  2. Calculate the average non-US revenue for all movies
  3. Select the non-US revenue for the first two movies in all_wars_matrix, store in non_us_all
  4. Use mean() again to print out the average of the values in non_us_some.
A
  1. # Select the non-US revenue for all movies
    • non_us_all <- all_wars_matrix[,2]
  2. # Average non-US revenue
    • mean(non_us_all)
  3. # Select the non-US revenue for first two movies
    • non_us_some <- all_wars_matrix[1:2,2]
  4. # Average non-US revenue for first two movies
    • mean(non_us_some)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

You are given the following:

  • all_wars _matrix (total sales in millions)
  • ticket_prices_matrix (ticket price for each movie)

Do the following:

  1. get the estimated number of US and non-US visitors for the six movies. Assign the result to visitors.
  2. From the visitors matrix, store the entire first column, representing the number of visitors in the US. Store this selection as us_visitors
  3. Calculate the average number of US visitors
A
  1. # Estimated number of visitors
    • visitors <- all_wars_matrix / ticket_prices_matrix
  2. # US visitors
    • us_visitors <- visitors[, 1]
  3. # Average number of US visitors
    • mean(us_visitors)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly