28

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.

flodel
  • 87,577
  • 21
  • 185
  • 223
lolibility
  • 2,187
  • 6
  • 25
  • 45

1 Answers1

51

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)
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • Oh, I only know header=FALSE, when I tried that in write.table, it says error. So I have to use col.names and row.names separatly. Thanks. – lolibility May 15 '12 at 20:44
  • 2
    @lolibility, do you know about R's help feature? If not you should try writing `?write.table` into your R console and then press enter. – Eric Fail May 15 '12 at 20:54
  • 1
    I used the write.table help and still came here in hopes of further clarification. – Patrick Williams Apr 24 '18 at 19:53