Related Articles

# 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:

 “Original data frame”

C1 C2

1  5 ab

2  6  b

3  7  C

4  8  d

 “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:

 “Original data frame”

C1  C2

1  5 1.2

2  6 6.0

3  7 5.7

4  8 0.5

 “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:

 “Original data frame”

C1 C2    C3

1  5  1 FALSE

2  6  6  TRUE

3  7  1 FALSE

 “matrix of the above data frame”

C1 C2 C3

[1,]  5  1  0

[2,]  6  6  1

[3,]  7  1  0

My Personal Notes arrow_drop_up