1. What’s matrix

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

matrix() function

Untitled

matrix(1:9, byrow = TRUE, nrow = 3)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
  1. Analyze matrices, you shall

Box office numbers of the star Wars franchise.

first element of each vector: US box office revenue

Second element: non-us box office

# 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,nrow = 3, byrow = TRUE)
  1. Naming a matrix

  2. calculating the world wide box office of three star wars movie

    1. Take the sum of the US revenue colum and the non-US revenue column.
    2. rowSums(my_matrix)
    3. worldwide_vector
  3. Adding a column for the worldwide box office

    #cbind, add a column or multiple columns to a matrix with the cbind()
    #big_matrix <- cbind(matrix1, matrix 2,
    # worldwide_vind vector as a column to star_wars_matrix
    star_wars_matrix <- cbind(worldwide_vector)
    
    # The worldwide box office figures
    worldwide_vector <- rowSums(star_wars_matrix)
    
    # Bind the new variable worldwide_vector as a column to star_wars_matrix
    all_wars_matrix <- cbind(star_wars_matrix, worldwide_vector)
    
  4. Adding a row

    1. cbind(), rbind()
    all_wars_matrix <- rbind(star_wars_matrix, star_wars_matrix2)
    US non-US
    A New Hope              461.0  314.4
    The Empire Strikes Back 290.5  247.9
    Return of the Jedi      309.3  165.8
    The Phantom Menace      474.5  552.5
    Attack of the Clones    310.7  338.7
    Revenge of the Sith     380.3  468.5
    
    >
    star_wars_matrix2 
                            US non-US
    The Phantom Menace   474.5  552.5
    Attack of the Clones 310.7  338.7
    Revenge of the Sith  380.3  468.5
    
  5. The total box office revenue for the entire saga