R Convert Matrix to Dataframe - Tutorial Kart

R Convert Matrix to Dataframe

In this tutorial, we will learn how to convert a matrix to a dataframe in R programming.

To convert Matrix to Dataframe in R, use as.data.frame() function. The syntax of as.data.frame() function is

as.data.frame(x, row.names = NULL, optional = FALSE,

make.names =asT.RdUatEa,....fra, me(x, row.names = NULL, optional = FALSE, stringsAsFamctaokres.n=ames = TRUE, ..., default.strinsgtrsinAgssFAascFtoarctors = default.stringsAsFactors()) s())

You can also provide row names to the dataframe using row.names.

Example to Convert Matrix to Dataframe in R

In this example, we will take a simple scenario wherein we create a matrix and convert the matrix to a dataframe.

> Mat1 = matrix(c(1, 5, 14, 23, 54, 9, 15, 85, 3, 42, 9, 7, 42, 87, 16), ncol=3) >>MMaatt11 = matrix(c(1, 5, 14, 23, 54, 9, 15, 85, 3, 42, 9, 7, 42, 87, 16), ncol=3)

[,1] [,2] [,3] [>1,M] at11 9 9 [2,] [,15] [,125] [,37] [3,] 14 85 42 [[41,,]] 1293 9 3 87 [>[52,D,]]F255=145 742 16 a[s3.,d]a1t4a.8fr5am42e(t(Mat1)) > DF2 [4V,1] V223 V3387V4 V5 1[5,1] 5541442213654 2 9 15 85 3 42 3> D9F27 =42as8.7da1t6a.frame(t(Mat1)) >> DF2

V1 V2 V3 V4 V5 1 1 5 14 23 54 2 9 15 85 3 42 3 9 7 42 87 16 >

You might be quizzed that the Dataframe looks like the transposed version of the Matrix. But, it is totally fine. It is just that these two data objects: matrix and dataframe are represented differently.

Example to Convert Matrix to Dataframe with Column Names

In this example, we create a matrix, and convert this matrix to a dataframe with row names.

> Mat1 = matrix(c(1, 5, 14, 23, 54, 9, 15, 85, 3, 42, 9, 7, 42, 87, 16), ncol=3) >>MMaatt11 = matrix(c(1, 5, 14, 23, 54, 9, 15, 85, 3, 42, 9, 7, 42, 87, 16), ncol=3)

[,1] [,2] [,3] [>1,M] at11 9 9 [2,] [,15] [,125] [,37] [3,] 14 85 42 [[41,,]] 1293 9 3 87 [>[52,D,]]F255=145 742 16 a[s3.,d]a1t4a.8fr5am42e(t(Mat1), row.names= c('name1', 'n[4a,m] e223',3'n8a7me3')) >[5D,]F254 42 16

V1 V2 V3 V4 V5 n>amDFe21 =1as5.d1a4ta2.3fr5a4me(t(Mat1), row.names= c('name1', 'name2', 'name3')) n>amDFe22 9 15 85 3 42 name3 9 7 42 87 16 > V1 V2 V3 V4 V5 name1 1 5 14 23 54 name2 9 15 85 3 42 name3 9 7 42 87 16 >

Concluding this R Tutorial, we have learned to convert a matrix to a dataframe, with the help of examples.

Home - Get Started

R Tutorial

R Script File

R Working Directory

R Data Types

R Variables

R Operators

R Vectors

R Matrix

Decision Making

R Decision Making

R if

R if..else

R if..else if...else

R switch

Loops

R Loops R repeat loop R while loop R for loop R break

Strings

R Strings Find length of String in R Extract Substring from a String in R Concatenate two or more Strings in R

Functions

R Functions

DataFrame

R Data Frame Sort R Data Frame by Column For each row in an R Data Frame Import Excel Data into R Dataframe Convert R Dataframe to Matrix R Dataframe - Delete Rows R Dataframe - Drop Columns R Dataframe - Add Column R Dataframe - Change Column Name R Dataframe - Remove Duplicate Rows R Dataframe - Replace NA with 0 Convert Matrix to R Dataframe

Handling Data from Files

R CSV Files - Read, Filter, Write R Read Excel XLS XLSX files

Charts & Graphs

R Pie Charts R Line Graphs

Statistical Analysis

R Mean of a Vector

R Median of a Vector

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download