writing a matrix to a file, without a header and row numbers

lolibility picture lolibility · May 15, 2012 · Viewed 57.2k times · Source

I have a matrix variable in R, say k. I want to write it out as a file. I use the code like:

write.table(k,file="outfile",sep="\t")

But when I get the file and open it, it contains headers. The first line is like: "v1" "v2" ...... "V6000". And after that, each line starts with the row number like "1","2" and so on. I don't want headers. Is there a way to do that?

And now I even cannot load the file into R again using read.table("outfile",header=TRUE,sep= "\t"), it's even not the same as what I previously outputed. R recognised the first colunm in the file which are row numbers as a new column.

Answer

Eric Fail picture Eric Fail · May 15, 2012

To remove row names and column names (header) when outputting a table to a text file, assign FALSE to both row.names and col.names when writing the matrix,

m <- matrix(1:12, 4 , 3)
write.table(m, file="outfile,txt", sep="\t", col.names = F, row.names = F)