In R, the matrix() function is a versatile tool for creating
matrices.
A matrix is a rectangular array of numbers, symbols, or
expressions, arranged in rows and columns.
#matrix(data, nrow, ncol, byrow = FALSE, dimnames = NULL)
Arguements:
• data: A vector containing the
elements of the matrix.
• nrow: The desired number
of rows.
• ncol: The desired number of columns.
• byrow: A logical value indicating whether the matrix
should be filled by row (TRUE) or by column (FALSE, default).
•
dimnames: A list of two character vectors, providing
row and column names.
Create a 3x3 matrix with numbers 1 to 9, filled by row:
my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)
print(my_matrix)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
Box office movies of Star Wars named new_hope, empire_strikes and return_jedi
# 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)
print(box_office)
## [1] 460.998 314.400 290.475 247.900 309.306 165.800
# Construct star_wars_matrix
star_wars_matrix <- matrix(box_office, nrow = 3, byrow = TRUE)
print(star_wars_matrix)
## [,1] [,2]
## [1,] 460.998 314.4
## [2,] 290.475 247.9
## [3,] 309.306 165.8
The colnames() function in R is a versitile tool used
to:
Retrieve Column Names:
• Returns a character vector
containing the names of the columns in a data frame or matrix.
Set Column Names:
• Assigns new names to the columns
of a data frame or matrix.
The new names must be provided as a
character vector of the same length as the number of columns.
The rownames() function in R is used to:
Retrieve Row Names:
• Returns a character vector
containing the names of the rows in a data frame or matrix.
Set Row Names:
• Assigns new names to the rows of a
data frame or matrix.
The new names must be provided as a character
vector of the same length as the number of rows.
# 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)
print(star_wars_matrix)
## [,1] [,2]
## [1,] 460.998 314.4
## [2,] 290.475 247.9
## [3,] 309.306 165.8
# 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
rowSums()The rowSums() function in R is used to calculate the sum
of values across each row of a matrix or data frame.
# Construct star_wars_matrix
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
region <- c("US", "non-US")
titles <- c("A New Hope",
"The Empire Strikes Back",
"Return of the Jedi")
# Create a matrix named as star_wars_matrix
star_wars_matrix <- matrix(box_office,
nrow = 3, byrow = TRUE,
dimnames = list(titles, region))
# Display the created 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
# Calculate worldwide box office figures
worldwide_vector <- rowSums(star_wars_matrix)
# Print the rowSums() result
worldwide_vector
## A New Hope The Empire Strikes Back Return of the Jedi
## 775.398 538.375 475.106
cbind()The cbind() function in R is a versatile tool used to
combine multiple objects (vectors, matrices, or data frames) by
columns.
This means it stacks the objects horizontally, creating a
new object with the combined columns.
# Construct star_wars_matrix
box_office <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)
region <- c("US", "non-US")
titles <- c("A New Hope",
"The Empire Strikes Back",
"Return of the Jedi")
star_wars_matrix <- matrix(box_office,
nrow = 3, byrow = TRUE,
dimnames = list(titles, region))
# The worldwide box office figures
worldwide_vector <- rowSums(star_wars_matrix)
# Print worldwide_vector rowSums
print(worldwide_vector)
## A New Hope The Empire Strikes Back Return of the Jedi
## 775.398 538.375 475.106
# Bind the new variable worldwide_vector as a column to star_wars_matrix
all_wars_matrix <- cbind(star_wars_matrix, worldwide_vector)
# Print result of cbind() of start_wars_matrix and worldwide_vector
print(all_wars_matrix)
## US non-US worldwide_vector
## A New Hope 460.998 314.4 775.398
## The Empire Strikes Back 290.475 247.9 538.375
## Return of the Jedi 309.306 165.8 475.106
colSums()colSums() in R: Summing Up Columns
The colSums() function in R is a handy tool to calculate
the sum of values within each column of a matrix or data frame.
# all_wars_matrix is available in your workspace
print(all_wars_matrix)
## US non-US worldwide_vector
## A New Hope 460.998 314.4 775.398
## The Empire Strikes Back 290.475 247.9 538.375
## Return of the Jedi 309.306 165.8 475.106
# 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 worldwide_vector
## 1060.779 728.100 1788.879
rbind()The rbind() function in R is used to combine multiple
objects (vectors, matrices, or data frames) by rows. This means it
stacks the objects vertically, creating a new object with the combined
rows.
### Adding Row: `rbind()` function
star_wars_vector <- c(461.0, 314.4, 290.5, 247.9, 309.3, 165.8)
region <- c("US", "non-US")
titles <- c("A New Hope", "The Empire Strike Back", "Return of the Jedi")
star_wars_matrix <- matrix(star_wars_vector,
nrow = 3, byrow = TRUE,
dimnames = list(titles, region))
print(star_wars_matrix)
## US non-US
## A New Hope 461.0 314.4
## The Empire Strike Back 290.5 247.9
## Return of the Jedi 309.3 165.8
star_wars_vector2 <- c(474.5, 552.5, 310.7, 338.7, 380.3, 468.5)
titles2 <- c("The Phantom Menace", "Attack of the Clones", "Revenge of the Sith")
star_wars_matrix2 <- matrix(star_wars_vector2,
nrow = 3, byrow = TRUE,
dimnames = list(titles2, region))
print(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)
# Print all_wars_matrix
print(all_wars_matrix)
## US non-US
## A New Hope 461.0 314.4
## The Empire Strike 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
In R, the matrix[] syntax is a powerful tool for
accessing and manipulating specific elements, rows, or columns within a
matrix.
matrix[row_index, column_index]
my_matrix[row_index, ]
my_matrix[, column_index]
# all_wars_matrix is available in your workspace
all_wars_matrix
## US non-US
## A New Hope 461.0 314.4
## The Empire Strike 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 <- all_wars_matrix[1:2, 2]
# Average non-US revenue for first two movies
mean(non_us_some)
## [1] 281.15
# all_wars_matrix is available in your workspace
all_wars_matrix
## US non-US
## A New Hope 461.0 314.4
## The Empire Strike 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
visitors
## US non-US
## A New Hope 92.20 62.88
## The Empire Strike 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
ticket_price <- c(5.0, 5.0, 6.0, 6.0, 7.0, 7.0, 4.0, 4.0, 4.5, 4.5, 4.9, 4.9)
star_wars_title <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi",
"The Phantom Menace", "Attack of the Clones", "Revenge of the Sith")
region <- c("US", "non-US")
ticket_prices_matrix <-
ticket_prices_matrix <- matrix(ticket_price, nrow = 6, byrow = TRUE,
dimnames = list(star_wars_title, region))
# 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 Strike 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