Professional Documents
Culture Documents
What is Matrix?
In R, a matrix is a collection of elements of the same data type such as numeric, character, or
logical). Moreover, these elements are arranged into a fixed number of rows and columns
(e.g., 3 rows and 3 columns, 10 rows, and 2 columns). This type of data is 2-dimensional.
These are some examples of matrices:
\ 5 3' 4 9
f
[l 4 5]
2 ,5 6 7j
As you can see, there are 5 parameters that you can use:
data - this argument is optional and will contain e.g. a vector of your data
nrow - this argument is used to get the number of rows, you want
ncol - this argument is, like nrow, but for the number of columns you want in your created
matrix
byrow - The byrow parameter is a logical clue. If its value is true, then the input vector
elements are arranged by row.
dimnames - this argument is used if you want to name the columns and rows
How to Create a Matrix in R ?
As you can see, in the above code, we used the seq() function to create a sequence in R (i.e., a
vector). Moreover, we used the nrow and ncol arguments to tell the matrix() function that we
want to create a three by three matrix. Here's how the matrix, called mtx, look like:
Here's how we can create a matrix in R, from a sequence of numbers (i.e., a vector) and get
the numbers by rows:
seq(7, 9))
Here's how to use the rbind() function to make a matrix from vectors in R:
Here in this, we need to pass both row and column to create an empty matrix: we need to
Output:
[,1]
[1,] NA
> I
Output:
[pi] CP2] [ T 3] [ i 4] [,5] [,6] [ i 7] [ i 8][ > 9]
C.io]
[1,] NA NA NA NA NA NA NA NA NA NA
[2,] NA NA NA NA NA NA NA NA NA NA
[3,] NA NA NA NA NA NA NA NA NA NA
[4,] NA NA NA NA NA NA NA NA NA NA
[5,] NA NA NA NA NA NA NA NA NA NA
[6,] NA NA NA NA NA NA NA NA NA NA
[7,] NA NA NA NA NA NA NA NA NA NA
[S,] NA NA NA NA NA NA NA NA NA NA
[9,] NA NA NA NA NA NA NA NA NA NA
[10,] NA NA NA NA NA NA NA NA NA NA
Where,
Example:
1
2 Matc-matrix(,nrow=10)
3
4 # printing empty matrix
5 print(Mat)
Output:
[,1]
[1,]
NA
[2,]
NA
[3,]
NA
[4,]
NA
15,1
NA
[6,]
NA
[7,]
NA
Method 3: Using only column.
[8,]
NA
[9,]
Here we need
NA to pass the only columns to create an empty matrix.
[10,
] NA
Where,
Here matrix name can be any valid identifier
Example:
1 Mat<-matrix(,ncol=10)
2
3 #printing empty matrix.
4 print(Mat)
5 I
Output:
[,£] [’,2] [i 3] [,4] [, 5] [,6] [,7] [,8] [,9] [,10] [1,] NA NA NA NA NA NA NA NA NA
NA
How to Name the Rows and Columns when Creating a Matrix in R ?
to name the rows and columns when creating a matrix we use the dimnames argument
In the code chunk above, we named the rows something very simple: 1, 2, and 3. Just for
fun, we named the columns "Vector", "List", and "Matrix".
mtx <- matrix(seq(l,9) ,
NAMING THE ROWS AND
nrow = 3, / COLUMNS WHEN
ncol = B, CREATING A MATRIX IN R
Like C and C++, we can easily access the elements of our matrix by using the index of the
element. There are three ways to access the elements from the matrix.
We can access the element which presents on nth row and mth column.
We can access all the elements of the matrix which are present on the nth row.
We can also access all the elements of the matrix which are present on the mth column.
Let see an example to understand how elements are accessed from the matrix present on
nth row mth column, nth row, or mth column.
1 # Defining the column and row names. row_names = cC'rowl'1, "row2",
2 "row3", ,Trow4") col_names = c("callM, "col2", "col3")
3 ^Creating matrix
4 R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names,
5 col_names))
6
7 pri nt(R)
8 I
9
^Accessing element present on 3rd row and 2nd column
1
print(R[3,2])
0
1 ^Accessing element present in 3rd row
1 printCR[3,])
1
2 ^Accessing element present in 2nd column
1 print(R[,2])
3
1
4
1
5
Output:
1
6
> ^Creating matrix
> R <- matrix(c(5:16), nrow = byrow = TRUE, dimnames = list(row_names, col_name
s))
4,
> print(R)
coll col 2 col 3
rowl 5 S 7
row 2 8 9 10
row 3 11 12 13
row4 14 15 16
Addition of rows
and columns
Use of relation
operators
matrix[n, m]<-y
Here, n and m are the rows and columns of the element, respectively. And, y is the value which
we assign to modify our matrix.
Example:
1 # Defining the column and row names.
2 row_names = cC'rowl", "row2", "row3", "row4")
3 col_names = c("coll", "col2", "col3")
4
5 R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names,
6 col_names))
7 print(R)
S
9 ^Assigning value 20 to the element| at 3d row and 2nd column LO R[3,2]<-20 LI
print(R)
Output: