A matrix is a two-dimensional, rectangular structure
of elements of the same type.
The elements can be accessed by their two-dimensional
position (row, column).
The rows and columns of the matrix might additionally be named and
consequently can be also accessed by a pair (rowname,
colname).
A matrix
can be manually created from a
vector.
Depending on the arguments, elements are put to the matrix in a
different order:
m <- matrix( 1:6, nrow = 2 );
m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
m <- matrix( 1:6, nrow = 2, byrow = TRUE );
m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
m <- matrix( 1:6, ncol = 2 );
m
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Test the class of m
:
class( m )
[1] "matrix" "array"
Try the function str
to get a compact summary of matrix
content:
str( m )
int [1:3, 1:2] 1 2 3 4 5 6
There are several functions to get dimensions of a matrix.
Number of columns is provided by:
ncol( m )
[1] 2
Number of rows is provided by:
nrow( m )
[1] 3
Dimensions (two elements: number of rows and number of columns):
dim( m )
[1] 3 2
colnames( m )
and rownames( m )
are used to
set and get the vectors with columns and rows names.
Try to set the names:
m
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
colnames( m ) <- c( "A", "B" )
m
A B
[1,] 1 4
[2,] 2 5
[3,] 3 6
rownames( m ) <- c( "X", "Y", "Z" )
m
A B
X 1 4
Y 2 5
Z 3 6
To get the names use:
rownames( m )
[1] "X" "Y" "Z"
colnames( m )
[1] "A" "B"
Use single brackets notation with two arguments
[ row(s), col(s) ]
to get/set
elements.
Note, when getting only a single row or a single column the result is
returned as a vector, not matrix.
m[ 3, 1 ] # a single element as a vector
[1] 3
m[ 3, ] # a single row as a vector
A B
3 6
m[ , 1 ] # a single column as a vector
X Y Z
1 2 3
m[ c( 2, 3 ), 1 ] # two rows/one column, still a vector
Y Z
2 3
m[ c( 2, 3 ), c( "B", "A" ) ] # a matrix
B A
Y 5 2
Z 6 3
m[ c( F, T, T ), c( "B", "A" ) ] # a matrix
B A
Y 5 2
Z 6 3
Notice the difference in the output class when only a single element is requested:
class( m[ 3, 1 ] )
[1] "integer"
class( m[ c( 2, 3 ), c( "B", "A" ) ] )
[1] "matrix" "array"
With the drop
argument you may enforce that always a
matrix is returned (never a vector):
m[ 3, 1 ] # a vector
[1] 3
m[ 3, 1, drop = FALSE ] # a matrix
A
Z 3
class( m[ 3, 1, drop = FALSE ] )
[1] "matrix" "array"
Empty row/column argument field means all rows and/or all columns:
m[ , c( "B", "A" ) ] # all rows, selected columns
B A
X 4 1
Y 5 2
Z 6 3
m[ c( "Z", "X", "Y" ), ] # selected rows, all columns
A B
Z 3 6
X 1 4
Y 2 5
m[ , ] # all rows and columns, same as just "m"
A B
X 1 4
Y 2 5
Z 3 6
Short summary of matrix operations: http://www.statmethods.net/advstats/matrix.html
m <- matrix( 1:6, nrow = 3 );
colnames( m ) <- c( "A", "B" )
rownames( m ) <- c( "X", "Y", "Z" )
m
A B
X 1 4
Y 2 5
Z 3 6
t(m)
X Y Z
A 1 2 3
B 4 5 6
m
A B
X 1 4
Y 2 5
Z 3 6
t( m )
X Y Z
A 1 2 3
B 4 5 6
m %*% t( m )
X Y Z
X 17 22 27
Y 22 29 36
Z 27 36 45
m
A B
X 1 4
Y 2 5
Z 3 6
m+1
A B
X 2 5
Y 3 6
Z 4 7
m * (m+1)
A B
X 2 20
Y 6 30
Z 12 42
m
A B
X 1 4
Y 2 5
Z 3 6
rowMeans( m )
X Y Z
2.5 3.5 4.5
rowSums( m )
X Y Z
5 7 9
colMeans( m )
A B
2 5
colSums( m )
A B
6 15
Copyright © 2024 Biomedical Data Sciences (BDS) | LUMC