0% found this document useful (0 votes)
29 views2 pages

Question 4: How To Create Matrices and Access Matrix Elements in R (Show Commands)

1) A matrix is a two-dimensional data structure that can be created using the matrix() function by passing data and specifying the number of rows and columns. 2) Elements of a matrix can be accessed using square bracket indexing, specifying the row and column numbers or vectors of rows and columns. 3) If the row or column is not specified during indexing, it will return the entire row or column. Negative integers can exclude rows or columns.

Uploaded by

Tarunvir Kukreja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Question 4: How To Create Matrices and Access Matrix Elements in R (Show Commands)

1) A matrix is a two-dimensional data structure that can be created using the matrix() function by passing data and specifying the number of rows and columns. 2) Elements of a matrix can be accessed using square bracket indexing, specifying the row and column numbers or vectors of rows and columns. 3) If the row or column is not specified during indexing, it will return the entire row or column. Negative integers can exclude rows or columns.

Uploaded by

Tarunvir Kukreja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 4: How to create matrices and access matrix elements in R (show

commands).
Ans-Matrix is a two dimensional data structure in R programming.Matrix is similar to vector but additionally
contains the dimension attribute. All attributes of an object can be checked with the attributes() function
(dimension can be checked directly with the dim() function). We can check if a variable is a matrix or not
with the class() function.
>a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> class(a)
[1] "matrix"
> attributes(a)
$dim
[1] 3 3
> dim(a)
[1] 3 3

Creating a matrix in R programming


Matrix can be created using the matrix() function.
Dimension of the matrix can be defined by passing appropriate value for arguments nrow and ncol.
Providing value for both dimension is not necessary. If one of the dimension is provided, the other
is inferred from length of the data.
> matrix(1:9, nrow = 3, ncol = 3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> # same result is obtained by providing only one dimension
> matrix(1:9, nrow = 3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
We can see that the matrix is filled column-wise. This can be reversed to row-wise filling by
passing TRUE to the argument byrow.
> matrix(1:9, nrow=3, byrow=TRUE) # fill matrix row-wise
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
In all cases, however, a matrix is stored in column-major order internally as we will see in the
subsequent sections.
It is possible to name the rows and columns of matrix during creation by passing a 2 element list to the
argument dimnames.
Accessing Elements of a matrix
We can access elements of a matrix using the square bracket [ indexing method. Elements can be accessed as
var[row, column]. Here rows and columns are vectors.
Using integer vector as index
We specify the row numbers and column numbers as vectors and use it for indexing.
If any field inside the bracket is left blank, it selects all.
We can use negative integers to specify rows or columns to be excluded.
>x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> x[c(1,2),c(2,3)] # select rows 1 & 2 and columns 2 & 3
[,1] [,2]
[1,] 4 7
[2,] 5 8
> x[c(3,2),] # leaving column field blank will select entire columns
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 2 5 8
> x[,] # leaving row as well as column field blank will select entire matrix
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> x[-1,] # select all rows except first
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 3 6 9
One thing to notice here is that, if the matrix returned after indexing is a row matrix or column
matrix, the result is given as a vector.
> x[1,]
[1] 1 4 7
> class(x[1,])
[1] "integer"
This behavior can be avoided by using the argument drop = FALSE while indexing.
> x[1,,drop=FALSE] # now the result is a 1X3 matrix rather than a vector
[,1] [,2] [,3]
[1,] 1 4 7
> class(x[1,,drop=FALSE])
[1] "matrix"
It is possible to index a matrix with a single vector.
While indexing in such a way, it acts like a vector formed by stacking columns of the matrix one
after another. The result is returned as a vector

You might also like