Convert an Object into a Matrix in R Programming - as.matrix() Function Last Updated : 30 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The as.matrix() function within R converts objects of various classes into a matrix. This can be helpful to work with structures of various data that can be converted into the matrix structure so that it becomes easier to analyze.Syntax: as.matrix(x)Parameters: x: Object to be convertedExample 1: Convert vector to the matrix using as.matrix() R # Creating a vector x <- c(1:9) # Calling as.matrix() Function as.matrix(x) Output [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 Example 2: Convert a Data Frame to a Matrix R # Calling pre-defined data set BOD # Calling as.matrix() Function as.matrix(BOD) Output Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 Time demand [1,] 1 8.3 [2,] 2 10.3 [3,] 3 19.0 [4,] 4 16.0 [5,] 5 15.6 ...Example 3: Convert a Sparse Matrix to a Dense Matrix R library(Matrix) # Create a sparse matrix s_mat <- Matrix(c(0, 0, 0, 0, 0, 0, 1, 2, 0), nrow = 3, ncol = 3) s_mat # Convert to a dense matrix d_mat <- as.matrix(s_mat) d_mat Output3 x 3 sparse Matrix of class "dtCMatrix" [1,] . . 1[2,] . . 2[3,] . . . [,1] [,2] [,3][1,] 0 0 1[2,] 0 0 2[3,] 0 0 0Example 4: Convert a SpatialPointsDataFrame to a Matrix R library(sp) # Create a SpatialPointsDataFrame co <- cbind(c(1, 2, 3), c(4, 5, 6)) sdf <- SpatialPointsDataFrame(coords = co, data = data.frame(ID = 1:3)) sdf # Convert to a matrix of coordinates c_mat <- as.matrix(co) c_mat Output coordinates ID1 (1, 4) 12 (2, 5) 23 (3, 6) 3 [,1] [,2][1,] 1 4[2,] 2 5[3,] 3 6 Comment More infoAdvertise with us Next Article Convert an Object into a Matrix in R Programming - as.matrix() Function N nidhi_biet Follow Improve Article Tags : R Language R Object-Function R Matrix-Function Similar Reads Convert an Object to List in R Programming - as.list() Function as.list() function in R Programming Language is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and dataframes. Syntax: as.list(object) Parameters: object: Vector, Matrix, factor, or data frame R - as.list() Function ExampleExample 1: Converting Vector to list 2 min read Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: Python3 1== # R program to convert a data frame # into a nu 2 min read Convert an Object into a Vector in R Programming - as.vector() Function as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: Python3 1== # R program to convert an object to vector # Creating an array x <- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function 1 min read Check if the Object is a Matrix in R Programming - is.matrix() Function is.matrix() function in R Language is used to return TRUE if the specified data is in the form of matrix else return FALSE. Syntax: is.matrix(x) Parameters: x: specified matrix Example 1: Python3 # R program to illustrate # is.matrix function # Specifying some different types of arrays A <- matri 1 min read Convert an Object to a Table in R Programming - as.table() Function as.table() function in R Language is used to convert an object into a table. Syntax: as.table(x) Parameters: x: Object to be converted Example 1: Python3 1== # R Program to convert # an object to a table # Creating a vector vec = c(2, 4, 3, 1, 2, 3, 2, 1, 4, 2) # Calling as.table() Function as.table 1 min read Like