R - Matrix

> Procedural Languages > R

1 - About

The matrix object in R is an array of two dimensions with the same class (data type).

Advertising

3 - Constructor

You can create a matrix with three methods:

  • the matrix method
  • the columns and rows bindings methods
  • the conversion of a vector in a matrix

3.1 - Matrix Function

matrix(data = NA, ncol = 1, nrow = length(data)/ncol, byrow = FALSE, dimnames = NULL)

where the attribute:

  • data is an data vector (including a list or expression vector).
  • ncol is the number of rows (default: 1)
  • nrow is the number of rows (default: the length to fill the data in the matrix: length(data)/ncol)
  • byrow describes how the matrix is filled (default by row)
  • dimnames to describe the column and row headers of the matrix: NULL or a list of length 2 giving the row and column names respectively.

More:

?matrix
Advertising

3.2 - (Columns of Rows) Vector Binding

# v1, ..., vn becomes columns of the matrix
cbind(v1, ..., vn) 
# v1, ..., vn becomes rows of the matrix
rbind(v1, ..., vn) 

where:

  • v1 and vn are vectors

3.3 - Vector to matrix

With the following vector:

> x
[1] 1 2 3 4 5 6
> is.vector(x)
[1] TRUE

you can transform it as a matrix by applying dimensionality:

> dim(m)=c(2,3)
> m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]   11   12   13
# is.matrix returns TRUE if x is a vector and has a "dim" attribute of length 2 and FALSE otherwise. 
> is.matrix(x)
[1] TRUE
Advertising

4 - Example

4.1 - Matrix Constructor

4.1.1 - Without Data

matrix(,3,3)
     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA
[3,]   NA   NA   NA

4.1.2 - Byrow

  • Matrix with Data filled by columns
matrix(1:9,3,3,FALSE)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
  • Matrix with Data filled by rows
matrix(1:9,3,3,TRUE)
      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

4.1.3 - Dimnames

  • Matrix with data and dimensions (row and column names)
> dimnames = list(c("Row1", "Row2"), c("Col1", "Col2", "Col3"))
> m <- matrix(c(1,11,2,12,3,13), nrow = 2, ncol = 3, dimnames = dimnames)
     Col1 Col2 Col3
Row1    1    2    3
Row2   11   12   13

4.1.4 - dimensionality

4.1.4.1 - default
  • Matrix without dimensionality (default is 1 columns)
matrix(1:6)
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
[6,]    6
4.1.4.2 - ncol

Matrix with only the number of columns

> m=matrix(1:6,ncol=2)
> m
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

4.2 - Columns and rows binding

With the following two vectors:

> v1=c(1,2,3)
> v2=c(4,5,6)
  • Matrix creation with Columns Binding
> m=cbind(v1,v2)
> m
     v1 v2
[1,]  1  4
[2,]  2  5
[3,]  3  6

* Matrix creation with Rows Binding

> m=rbind(v1,v2)
> m
   [,1] [,2] [,3]
v1    1    2    3
v2    4    5    6

5 - Operations

5.1 - Mathematical

Matrix Operations

s = seq (1,4)
m=matrix(s,2,2)
m
     [,1] [,2]
[1,]    1    3
[2,]    2    4
s2 = seq (4,1)
m2 = matrix(s2,2,2)
m2
     [,1] [,2]
[1,]    4    2
[2,]    3    1
m*m2
     [,1] [,2]
[1,]    4    6
[2,]    6    4
m-m2
     [,1] [,2]
[1,]   -3    1
[2,]   -1    3
m/m2
          [,1] [,2]
[1,] 0.2500000  1.5
[2,] 0.6666667  4.0
m^m2
     [,1] [,2]
[1,]    1    9
[2,]    8    4

5.2 - Subset

6 - How to

6.1 - Check if it's a Matrix

6.2 - Check the attributes

With the following matrix,

     Col1 Col2 Col3
Row1    1    2    3
Row2   11   12   13

you can check the attributes with the attributes function.

> attributes(m)
$dim
[1] 2 3

$dimnames
$dimnames[[1]]
[1] "Row1" "Row2"

$dimnames[[2]]
[1] "Col1" "Col2" "Col3"

6.3 - Get the numbers of rows and/of columns

  • Number of columns
> ncol(m)
[1] 3
  • Number of rows
> nrow(m)
[1] 2

6.4 - Get the values of a cell

By indexing, you can retrieve values (by default as a vector).

m=matrix(1:6,ncol=2)
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
  • Indexing: Return a vector of the cell value located in the second row, first column:
m[2,1]
[1] 2
  • Indexing without y coordinates. To return a vector of the second rows:
m[2,]
[1] 2 5
  • Indexing (returning a matrix)
 m[1, , drop = FALSE]
     [,1] [,2]
[1,]    1    4

6.5 - Change the dimensionality of a matrix

> m=matrix(1:6,ncol=2)
> m
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
> dim(m)=c(2,3)
> m
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

6.6 - Remove the NA values

lang/r/matrix.txt · Last modified: 2017/09/13 11:46 by gerardnico