Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export R matrix object to .txt file

Tags:

r

I have a R object say:

mat <- matrix(1:100,nrow=20)

I want to send this matrix to .txt file so that the exported .txt file contains 20 rows and 5 columns. Is there easy way to do this?

Thanks in advance,

like image 840
FairyOnIce Avatar asked Oct 16 '12 03:10

FairyOnIce


People also ask

How do you create a text file in R studio?

To create an R Markdown report, open a plain text file and save it with the extension . Rmd. You can open a plain text file in your scripts editor by clicking File > New File > Text File in the RStudio toolbar. Be sure to save the file with the extension .


2 Answers

No packages required:

write.table(mat, file="mymatrix.txt", row.names=FALSE, col.names=FALSE)

This will use space as a separator, but you can also add sep = ... if you prefer tabs or any other delimiters (replace ... with your desired delimiter, of course).

like image 76
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 05 '22 06:10

A5C1D2H2I1M1N2O1R2T1


The documentation is a great resource. :)

library(MASS)
mat <- matrix(1:100,nrow=20)
write.matrix(mat,'/path/to/file.txt',sep = "\t")
like image 32
ako Avatar answered Oct 05 '22 07:10

ako