Operations with Matrices
Matrices in R support arithmetic operations, both with single numbers and with built-in functions that summarize their values.
Scalar Operations
Applying an arithmetic operation with a single number affects every element in the matrix.
Example
1234m <- matrix(1:9, nrow = 3, byrow = TRUE) # Multiply every element by 3 m * 3
Aggregate Functions
Functions like sum() and mean() compute totals or averages across the entire matrix.
Example
123456m <- matrix(1:9, nrow = 3, byrow = TRUE) # Overall mean of all elements mean(m) # Overall sum of all elements sum(m)
Row and Column Functions
You can also apply these functions to rows or columns separately with rowSums(), rowMeans(), colSums(), and colMeans().
Example
123456m <- matrix(1:9, nrow = 3, byrow = TRUE) # Column sums colSums(m) # Row means rowMeans(m)
These functions make it easy to analyze matrix data by dimension.
Swipe to start coding
You have a matrix named m:
2 4 6 8
10 12 14 16
Your tasks are:
- Divide each element by
2, then add1. Reassign the result to themvariable. - Output the matrix
m. - Output the column totals of the
mmatrix. - Output the mean value of all
melements.
Рішення
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 2.27
Operations with Matrices
Свайпніть щоб показати меню
Matrices in R support arithmetic operations, both with single numbers and with built-in functions that summarize their values.
Scalar Operations
Applying an arithmetic operation with a single number affects every element in the matrix.
Example
1234m <- matrix(1:9, nrow = 3, byrow = TRUE) # Multiply every element by 3 m * 3
Aggregate Functions
Functions like sum() and mean() compute totals or averages across the entire matrix.
Example
123456m <- matrix(1:9, nrow = 3, byrow = TRUE) # Overall mean of all elements mean(m) # Overall sum of all elements sum(m)
Row and Column Functions
You can also apply these functions to rows or columns separately with rowSums(), rowMeans(), colSums(), and colMeans().
Example
123456m <- matrix(1:9, nrow = 3, byrow = TRUE) # Column sums colSums(m) # Row means rowMeans(m)
These functions make it easy to analyze matrix data by dimension.
Swipe to start coding
You have a matrix named m:
2 4 6 8
10 12 14 16
Your tasks are:
- Divide each element by
2, then add1. Reassign the result to themvariable. - Output the matrix
m. - Output the column totals of the
mmatrix. - Output the mean value of all
melements.
Рішення
Дякуємо за ваш відгук!
single