Convert DataFrame to Matrix with Column Names in R
Last Updated :
21 Apr, 2021
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 matrix
- rownames.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
Similar Reads
Convert list to dataframe with specific column names in R A list contains different types of objects as their components. The components may belong to different data types or different dimensions. Vector can be useful components of a list and can be easily mapped as the rows or columns of a dataframe. Each column in the dataframe is referenced using a uniq
4 min read
Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
3 min read
Convert dataframe column to list in R In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter
2 min read
Convert DataFrame Column to Numeric in R In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion,
9 min read
Create DataFrame with Spaces in Column Names in R In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language. Method 1: Using check.names attribute The data.frame() method in R can be used to create a data frame with individual rows and columns in R. This method contains an attribute check.names, wh
4 min read
Convert Matrix to Dataframe in R In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the
1 min read