DC Introduction to R - Matrices

# DC Introduction to R
DC Introduction to R - Intro to basics
DC Introduction to R - Vectors
DC Introduction to R - Matrices
DC Introduction to R - Factors
DC Introduction to R - Data frames
DC Introduction to R - Lists

1 What's a matrix?[ | ]

# Construct a matrix with 3 rows that contain the numbers 1 up to 9
matrix(1:9, byrow = TRUE, nrow = 3)
#     [,1] [,2] [,3]
#[1,]    1    2    3
#[2,]    4    5    6
#[3,]    7    8    9

2 Analyze matrices, you shall[ | ]

# 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)

3 Naming a 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)

# 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
print(star_wars_matrix)
#                             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

4 Calculating the worldwide box office[ | ]

# 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")))

# Calculate worldwide box office figures
worldwide_vector <- rowSums(star_wars_matrix)

5 Adding a column for the Worldwide box office[ | ]

# 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")))

# 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)

6 Adding a row[ | ]

# star_wars_matrix and star_wars_matrix2 are available in your workspace
star_wars_matrix
#                           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

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

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

7 The total box office revenue for the entire saga[ | ]

# all_wars_matrix is available in your workspace
all_wars_matrix
#                           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

# Total revenue for US and non-US
total_revenue_vector <- colSums(all_wars_matrix)
  
# Print out total_revenue_vector
print(total_revenue_vector)
#    US non-US 
#2226.3 2087.8

8 Selection of matrix elements[ | ]

# all_wars_matrix is available in your workspace
all_wars_matrix
#                           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

# Select the non-US revenue for all movies
non_us_all <- all_wars_matrix[,2]
  
# Average non-US revenue
mean(non_us_all)
#[1] 347.9667

# Select the non-US revenue for first two movies
non_us_some <- non_us_all[1:2]
  
# Average non-US revenue for first two movies
mean(non_us_some)
#[1] 281.15

9 A little arithmetic with matrices[ | ]

# all_wars_matrix is available in your workspace
all_wars_matrix
#                           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

# Estimate the visitors
visitors <- all_wars_matrix / 5
  
# Print the estimate to the console
print(visitors)
#                           US non-US
#A New Hope              92.20  62.88
#The Empire Strikes Back 58.10  49.58
#Return of the Jedi      61.86  33.16
#The Phantom Menace      94.90 110.50
#Attack of the Clones    62.14  67.74
#Revenge of the Sith     76.06  93.70

10 A little arithmetic with matrices (2)[ | ]

# all_wars_matrix and ticket_prices_matrix are available in your workspace
all_wars_matrix
#                           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

ticket_prices_matrix
#                         US non-US
#A New Hope              5.0    5.0
#The Empire Strikes Back 6.0    6.0
#Return of the Jedi      7.0    7.0
#The Phantom Menace      4.0    4.0
#Attack of the Clones    4.5    4.5
#Revenge of the Sith     4.9    4.9

# 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)
#[1] 75.01401

11 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}