Convert DataFrame to Matrix with Column Names in R Last Updated : 21 Apr, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Data frames and matrices are R objects, both of which allow tabular storage of the data into well organized cells. However, the data in a data frame can consist of different data types, that is the cells may contain data belonging to a combination of data types. Matrices, on the other hand, strictly allow a singular data type value to be stored across all its data elements. These are interconvertible into each other, if they satisfy the following conditions : Data frame should not have NA or missing values.The data type stored across all the columns should be of the same type, that is either numeric or character type. The data.matrix() method in R Programming language used to convert a data frame to a numeric matrix. All the variables contained in a data frame are translated to corresponding numeric modes followed by their binding to form columns of a matrix. However, the character values are not retained and are converted to equivalent integer values in order to preserve the uniformity of the data type of the matrix. The column names are preserved during the interconversion. Syntax: data.matrix(dataframe, rownames.force = NA) Parameters : dataframe - the data frame to convert to a matrixrownames.force - logical determining whether the resulting matrix should have character (rather than NULL) row names. Example 1: R # declaring a data frame in R data_frame = data.frame(C1= c(5:8), C2 = c("ab","b","C","d")) print("Original data frame") print(data_frame) # converting the data frame into matrix mat = data.matrix(data_frame) print ("matrix of the above data frame") print (mat) Output: [1] "Original data frame" C1 C2 1 5 ab 2 6 b 3 7 C 4 8 d [1] "matrix of the above data frame" C1 C2 [1,] 5 1 [2,] 6 2 [3,] 7 3 [4,] 8 4 However, there is an exception to this transformation, that is, in case the data supplied as input is completely numeric, including integers and floating-point decimals, then the same data is returned as output without any interconversion. Example 2: R # declaring a data frame in R data_frame = data.frame(C1= c(5:8),C2 = c(1.2,6,5.7,0.5)) print("Original data frame") print(data_frame) # converting the data frame into matrix mat = data.matrix(data_frame) print ("matrix of the above data frame") print (mat) Output: [1] "Original data frame" C1 C2 1 5 1.2 2 6 6.0 3 7 5.7 4 8 0.5 [1] "matrix of the above data frame" C1 C2 [1,] 5 1.2 [2,] 6 6.0 [3,] 7 5.7 [4,] 8 0.5 The boolean data type, TRUE is returned as 1 in the matrix form and FALSE as 0. Example 3: R # declaring a data frame in R data_frame = data.frame(C1= c(5:7),C2 = c(1,6,TRUE), C3=c(FALSE,TRUE,FALSE)) print("Original data frame") print(data_frame) # converting the data frame into matrix mat = data.matrix(data_frame) print ("matrix of the above data frame") print (mat) Output: [1] "Original data frame" C1 C2 C3 1 5 1 FALSE 2 6 6 TRUE 3 7 1 FALSE [1] "matrix of the above data frame" C1 C2 C3 [1,] 5 1 0 [2,] 6 6 1 [3,] 7 1 0 Comment More infoAdvertise with us Y yippeee25 Follow Improve Article Tags : R Language R Programs R-Matrix R-DataFrame R DataFrame-Programs R Matrix-Programs +2 More Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Like