How to Convert Data Frames to Matrices in R


How to Convert Data Frames to Matrices in R ?

Answer

In R, you can convert data frames to matrices using the as.matrix() function. This function transforms the data frame into a matrix by retaining the original data structure.



✐ Examples

1 Converting a Simple Data Frame to a Matrix

In this example,

  1. We start with a simple data frame named df containing three columns: col1, col2, and col3.
  2. We use the as.matrix() function to convert the data frame df into a matrix named mat.
  3. We print the resulting matrix mat to the console to verify the conversion.

R Program

df <- data.frame(col1 = c(1, 2, 3), col2 = c(4, 5, 6), col3 = c(7, 8, 9))
mat <- as.matrix(df)
print(mat)

Output

     col1 col2 col3
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

2 Converting a Data Frame with Factors to a Matrix

In this example,

  1. We start with a data frame named df containing two columns: name and age. The age column is a factor.
  2. We use the as.matrix() function to convert the data frame df into a matrix named mat.
  3. We print the resulting matrix mat to the console to verify the conversion.

R Program

df <- data.frame(name = c('Alice', 'Bob', 'Charlie'), age = factor(c('Young', 'Middle', 'Old')))
mat <- as.matrix(df)
print(mat)

Output

     name     age  
[1,] "Alice" "Young"
[2,] "Bob"   "Middle"
[3,] "Charlie" "Old"  

Summary

In this tutorial, we learned How to Convert Data Frames to Matrices in R language with well detailed examples.




More R Matrices Tutorials

  1. How to Create a Matrix in R ?
  2. How to Combine Matrices by Columns in R ?
  3. How to Combine Matrices by Rows in R ?
  4. How to Create Diagonal Matrices in R ?
  5. How to Access Matrix Elements using Indexing and Slicing in R ?
  6. How to get Matrix Size in R ?
  7. How to get Number of Rows in Matrix in R ?
  8. How to get Number of Columns in Matrix in R ?
  9. How to do Matrix Addition in R ?
  10. How to do Matrix Subtraction in R ?
  11. How to do Matrix Multiplication in R ?
  12. How to do Scalar Multiplication on a Matrix in R ?
  13. How to do Scalar Division on a Matrix in R ?
  14. How to do Element-Wise Operations in a Matrix in R ?
  15. How to Assign Row and Column Names in a Matrix in R ?
  16. How to get Row Names in a Matrix in R ?
  17. How to get Column Names in a Matrix in R ?
  18. How to find Transpose of a Matrix in R ?
  19. How to Extract the Diagonal of a Matrix in R ?
  20. How to find Determinant of a Matrix in R ?
  21. How to find Inverse of a Matrix in R ?
  22. How to find Rank of a Matrix in R ?
  23. How to Find Eigenvalues and Eigenvectors of a Matrix in R ?
  24. How to Perform Singular Value Decomposition (SVD) in R ?
  25. How to Perform QR Decomposition in R ?
  26. How to Perform Cholesky Decomposition in R ?
  27. How to Reshape Matrices in R ?
  28. How to Convert Data Frames to Matrices in R ?
  29. How to Create Identity Matrix in R ?
  30. How to Create Zero Matrix in R ?
  31. How to Create Ones Matrix in R ?
  32. How to Generate Random Matrices in R ?
  33. How to Calculate Row and Column Sums in R ?
  34. How to Calculate Row and Column Means in R ?
  35. How to Find Row and Column Max/Min in R ?
  36. How to Perform Element-Wise Comparisons in R ?