Matrix in R Programming
A matrix is a two-dimensional array in which each element has the same mode (numeric, character, or logical). Matrices are created with the matrix() function.
Syntax :
Mymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
byrow=logical_value, dimnames=list(char_vector_rownames, char_vector_colnames))
where vector contains the elements for the matrix, nrow and ncol specify the row and
column dimensions, and dimnames contains optional row and column labels stored in
character vectors. The option byrow indicates whether the matrix should be filled in by row (byrow=TRUE) or by column (byrow=FALSE). The default is by column.
The following listing demonstrates the matrix function.
> y <- matrix(1:20, nrow=5, ncol=4)
> y
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
To see dimension of the matrix, you can use dim function.>dim(y)
[1] 5,3
Another example of Matrix..
> myvector<- c(1,26,24,68)
> rnames <- c("R1", "R2")
> cnames <- c("C1", "C2")
> mymatrix <- matrix(myvector, nrow=2, ncol=2, byrow=TRUE,dimnames=list(rnames, cnames))
> mymatrix
C1 C2
R1 1 26
R2 24 68
> mymatrix <- matrix(myvector, nrow=2, ncol=2, byrow=FALSE,dimnames=list(rnames, cnames))
> mymatrix
C1 C2
R1 1 24
R2 26 68
First you create a 5 × 4 matrix b. Then you create a 2 × 2 matrix with labels and fill the matrix by rows c. Finally, you create a 2 × 2 matrix and fill the matrix by columns d.
Matrix subscripts :
We can identify rows, columns, or elements of a matrix by using the subscripts and brackets. X[i,] refers to the i th row of matrix X, X[,j] refers to the j th column, and X[i, j] refers to the ij th element, respectively. The subscripts i and j can be numeric vectors in order to select multiple rows or columns.
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]
[1] 7
> x[1, c(4,5)]
[1] 7 9
Here, First a 2 × 5 matrix is created containing the numbers 1 to 10. By default,the matrix is filled by column.
Then the elements in the second row are selected, followed by the elements in the second column.
Next, the element in the first row and fourth column is selected. Finally, the elements in the first row and the fourth and fifth columns are selected.
Matrices are two-dimensional and, like vectors, can contain only one data type.
Note:
When there are more than two dimensions, you use arrays.When there are multiple modes of data, you use data frames.
A matrix is a two-dimensional array in which each element has the same mode (numeric, character, or logical). Matrices are created with the matrix() function.
Syntax :
Mymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
byrow=logical_value, dimnames=list(char_vector_rownames, char_vector_colnames))
where vector contains the elements for the matrix, nrow and ncol specify the row and
column dimensions, and dimnames contains optional row and column labels stored in
character vectors. The option byrow indicates whether the matrix should be filled in by row (byrow=TRUE) or by column (byrow=FALSE). The default is by column.
The following listing demonstrates the matrix function.
> y <- matrix(1:20, nrow=5, ncol=4)
> y
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
To see dimension of the matrix, you can use dim function.>dim(y)
[1] 5,3
Another example of Matrix..
> myvector<- c(1,26,24,68)
> rnames <- c("R1", "R2")
> cnames <- c("C1", "C2")
> mymatrix <- matrix(myvector, nrow=2, ncol=2, byrow=TRUE,dimnames=list(rnames, cnames))
> mymatrix
C1 C2
R1 1 26
R2 24 68
> mymatrix <- matrix(myvector, nrow=2, ncol=2, byrow=FALSE,dimnames=list(rnames, cnames))
> mymatrix
C1 C2
R1 1 24
R2 26 68
First you create a 5 × 4 matrix b. Then you create a 2 × 2 matrix with labels and fill the matrix by rows c. Finally, you create a 2 × 2 matrix and fill the matrix by columns d.
Matrix subscripts :
We can identify rows, columns, or elements of a matrix by using the subscripts and brackets. X[i,] refers to the i th row of matrix X, X[,j] refers to the j th column, and X[i, j] refers to the ij th element, respectively. The subscripts i and j can be numeric vectors in order to select multiple rows or columns.
> x <- matrix(1:10, nrow=2)
> x[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]
[1] 7
> x[1, c(4,5)]
[1] 7 9
Here, First a 2 × 5 matrix is created containing the numbers 1 to 10. By default,the matrix is filled by column.
Then the elements in the second row are selected, followed by the elements in the second column.
Next, the element in the first row and fourth column is selected. Finally, the elements in the first row and the fourth and fifth columns are selected.
Matrices are two-dimensional and, like vectors, can contain only one data type.
Note:
When there are more than two dimensions, you use arrays.When there are multiple modes of data, you use data frames.
--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.