Open In App

Convert DataFrame to Matrix with Column Names in R

Last Updated : 21 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
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 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 unique name, which can be either equivalent to the lis
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 contains the name of the column with the help of th
3 min read
Convert Values in Column into Row Names of DataFrame in R
The row names in R are by default mapped to the row numbers, beginning with the integer value 1. The row names can be modified easily and reassigned to any possible string vector to assign customized names. Since, the row numbers are practically equal in each column of the dataframe, therefore the column values can also be assigned to the row names
4 min read
Replace Spaces in Column Names in R DataFrame
In this article, we will replace spaces in column names of a dataframe in R Programming Language. Let's create a Dataframe with 4 columns with 3 rows: C/C++ Code # create a dataframe with 4 columns and 3 rows data = data.frame("web technologies" = c("php","html","js"), "backend tech" = c("sql
2 min read
How to add a prefix to column names in R DataFrame ?
In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language. Dataset in use: First SecondThird1a72ab83cv94dsd10Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used. The paste() method, can be used for the concatenation of string vectors together to f
4 min read
How to add suffix to column names in R DataFrame ?
Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used.
4 min read
How to assign column names based on existing row in R DataFrame ?
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all components. Every element is the same as each of the value
3 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, which is by default set to TRUE while making this ca
4 min read
Create empty DataFrame with only column names in R
In this article, we are going to discuss how to create a data frame in r with column names and create an empty data frame with column names in the R Programming Language. The basic syntax for creating a data frame is using data. frame(). Create a Data Frame with Values and column Names C/C++ Code # Define the data id <- c(1, 2, 3) names <- c(
3 min read
Extract Values from Matrix by Column and Row Names in R
In this article, we will discuss how to extract values from the matrix by column and row names in R Programming Language. Extracting values from matrix by Column names A row subset matrix can be extracted from the original matrix using a filter for the selected row names. Since a matrix's elements are accessed in a dual index format, particular row
4 min read