Convert Matrix to Dataframe in R
Last Updated :
09 May, 2021
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 matrix and convert it to each column in the dataframe.
Syntax:
as.data.frame(matrix_data)
Where, matrix_data is the input matrix.
Example 1:
R
matrix_data= matrix ( c (1,2,3,4,5,6,7,8),nrow=4)
print (matrix_data)
dataframe_data= as.data.frame (matrix_data)
print (dataframe_data)
|
Output:
Example 2:
R
matrix_data= matrix ( c (1,2,3,4,5,6,7,8,11:18),nrow=8)
print (matrix_data)
dataframe_data= as.data.frame (matrix_data)
print (dataframe_data)
|
Output:
Example 3:
R
matrix_data= matrix ( c (
"bobby" , "pinkey" , "rohith" , "gnanesh" ,5.3,6.6,7,8,11:18),nrow=8)
print (matrix_data)
dataframe_data= as.data.frame (matrix_data)
print (dataframe_data)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...