Saving matrix to .txt file with correct alignment

I am trying to save the matrix that I have in R as a text file with a tab delimiter with the names of the rows and columns that are included and correctly aligned.

I tried this:

write.table(data, "mytable.txt", sep="\t", col.names=TRUE)

But when I open the file, it is a messy mess. I assume this has something to do with row or column names that are not specified, but I'm not sure.

+4
source share
2 answers

You can use the following code to write the output of your matrix to a file using the neat format you see when printing from the R console:

max.print <- getOption('max.print')
options(max.print=nrow(data) * ncol(data))
sink('data.txt')
data
sink()
options(max.print=max.print)

data.txt. , File -> Open script R. , .

: SO, .

+1

write.table CSV ( ):

CSV. . col.names = NA row.names = TRUE,

row.names = TRUE , col.names=TRUE col.names=NA, .

0

Source: https://habr.com/ru/post/1609402/


All Articles