R - Matrix

Card Puncher Data Processing

About

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

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

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

(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

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

Example

Matrix Constructor

Without Data

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

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

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

dimensionality

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

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

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

Operations

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

Subset

See R - Subset Operators (Extract or Replace Parts of an Object)

How to

Check if it's a Matrix

is.matrix(x)

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"

Get the numbers of rows and/of columns

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

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

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

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

Remove the NA values

See R - NA (Not Available)





Discover More
Card Puncher Data Processing
R - (Object|Variable|Symbol)

R provides a number of specialized objects. They are created (instantiated), used and referenced through variable (known as symbol). When you read the term object in the documentation, you can interchange...
Card Puncher Data Processing
R - Apply a function over a data structure

The apply function call a function over a data structure: apply do it over a array or matrix lapply do it over a list ...
Card Puncher Data Processing
Backlinks

A class is just an attribute of an object. You can remove the attribute with the function . The class with then become the type A list can have different class. unclass returns (a copy of) its...
R Studio Import Dataset
R - Data frame Object

A data frame is a logical implementation of a table in a relational database A data frame inherits all the property and function of an object. It has a list of variables of the same number of rows with...
Card Puncher Data Processing
R - K-Nearest Neighbors (KNN) Analysis

in R. where: k is number of neighbours to be considered. train is the training set c1 is the factor of the training set with the true target test is the test set The knn function is...
Card Puncher Data Processing
R - List

The list in R may contain elements of the different class (just like a data frame) class a vector (1 dimension) or a matrix (2 dimensions) data.tables and data.frames are internally lists with all...
Lasso Cv Plot
R - Shrinkage Method (Ridge Regression and Lasso)

Unlike subset and forward stepwise regression, which controls the complexity of a model by restricting the number of variables, ridge regression keeps all the variables in and shrinks the coefficients...
Card Puncher Data Processing
R - Subset Operators (Extract or Replace Parts of an Object)

In the Data Manipulation category, you have the subset operators: [ [[ $ This operators acts on: vectors, matrices, arrays lists data.frame to extract or replace parts. See also:...
Card Puncher Data Processing
R - Vector

The Vector in R contains elements of the same class. A Vector with different class of objects is a list. A vector (as every object) can also have . Almost all data in R is a vector or is based upon...



Share this page:
Follow us:
Task Runner