- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to save matrix created in R as tables in a text file with column names same as the matrix?

Matrix data is sometimes need to be saved as table in text files, the reason behind this is storage capacity of text files. But when we save a matrix as text files in R, the column names are misplaced therefore we need to take care of those names and it can be done by setting column names to the desired value.

> M<-matrix(1:16,nrow=4) > M [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > colnames(M)<-c("A1","A2","A3","A4") > rownames(M)<-c("R1","R2","R3","R4") > M A1 A2 A3 A4 R1 1 5 9 13 R2 2 6 10 14 R3 3 7 11 15 R4 4 8 12 16 > write.table(M, 'M.txt')

This file will be saved in documents folder of your system and the output will look like below −

## Output

Now, here the column name A1 is above the rows but we don’t want it to be in this form because it is making our last column A4 blank. Therefore, we need to save the matrix M as table in a way that will be same as we have in R. It can be done as shown below −

> write.table(M, 'M.txt', col.names=NA)

Now the saved file should look like the below −

## Output

The main objective is to save the file with the column names above their values otherwise it will be confusing to read the table.

Let’s have a look at another example −

: > new_matrix<-matrix(letters[1:16],nrow=4) > new_matrix [,1] [,2] [,3] [,4] [1,] "a" "e" "i" "m" [2,] "b" "f" "j" "n" [3,] "c" "g" "k" "o" [4,] "d" "h" "l" "p" > colnames(new_matrix)<-c("C1","C2","C3","C4") > rownames(new_matrix)<-c("R1","R2","R3","R4") > new_matrix C1 C2 C3 C4 R1 "a" "e" "i" "m" R2 "b" "f" "j" "n" R3 "c" "g" "k" "o" R4 "d" "h" "l" "p" > write.table(new_matrix, 'new_matrix.txt', col.names=NA)

The saved file will look like the below one −

## Output

- Related Questions & Answers
- How to save a matrix as CSV file using R?
- How to convert a matrix into a data frame with column names and row names as variables in R?
- How to convert a matrix to a data frame with column names and row names as new columns in R?
- How to save a plot as SVG created with ggplot2 in R?
- How to save a vector in R as CSV file?
- How to create a matrix with vectors as elements in R?
- How to remove the row names or column names from a matrix in R?
- How to find the column names and row names from a matrix in R?
- Get only the file extension from a column with file names as strings in MySQL?
- How to save an R data frame as txt file?
- How to change the repeated row names and column names to a sequence in a matrix in R?
- How to convert a matrix into a matrix with single column in R?
- How to plot matrix columns as lines in base R?
- How to find the row and column position of a value as vector in an R matrix?
- How to save the str output as a string in R?