Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function Last Updated : 16 Jun, 2020 Comments Improve Suggest changes Like Article Like Report 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 numeric matrix # Creating a dataframe df1 = data.frame( "Name" = c("Amar", "Akbar", "Ronald"), "Language" = c("R", "Python", "C#"), "Age" = c(26, 38, 22) ) # Printing data frame print(df1) # Converting into numeric matrix df2 <- data.matrix(df1) df2 Output: Name Language Age 1 Amar R 26 2 Akbar Python 38 3 Ronald C# 22 Name Language Age [1, ] 2 3 26 [2, ] 1 2 38 [3, ] 3 1 22 Example 2: Python3 1== # R program to convert a data frame # into a numeric matrix # Creating a dataframe df <- data.frame(sample(LETTERS[1:4], 8, replace = T), cbind(1:4, 1:8)) colnames(df) <- c("x", "y", "z") # Printing data frame print(df) # Converting into numeric matrix df2 <- data.matrix(df) df2 Output: x y z 1 A 1 1 2 D 2 2 3 C 3 3 4 A 4 4 5 B 1 5 6 B 2 6 7 A 3 7 8 C 4 8 x y z [1, ] 1 1 1 [2, ] 4 2 2 [3, ] 3 3 3 [4, ] 1 4 4 [5, ] 2 1 5 [6, ] 2 2 6 [7, ] 1 3 7 [8, ] 3 4 8 Comment More infoAdvertise with us Next Article Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function N nidhi_biet Follow Improve Article Tags : R Language R DataFrame-Function R Matrix-Function Similar Reads Convert an Object into a Matrix in R Programming - as.matrix() Function 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: Co 2 min read Convert an Object to Data Frame in R Programming - as.data.frame() Function as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters:Â object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic exam 2 min read Create Matrix and Data Frame from Lists in R Programming In R programming, there 5 basic objects. Lists are the objects that can contain heterogeneous types of elements, unlike vectors. Matrices can contain the same type of elements or homogeneous elements. On the other hand, data frames are similar to matrices but have an advantage over matrices to keep 3 min read Convert a Vector into Factor in R Programming - as.factor() Function as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters:Â Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in RR # Creating a vector x<-c("female", "male", "ma 1 min read Convert Factor to Numeric and Numeric to Factor in R Programming Factors are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. Though factors may look similar to character vectors, they are integers and care m 5 min read Like