将矩阵写入文件,没有标题和行号

时间:2022-09-04 21:44:52

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

我在R中有一个矩阵变量,比如k。我想把它写成一个文件。我使用的代码如下:

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?

但是当我获取文件并打开它时,它包含标题。第一行是:“v1”“v2”......“V6000”。之后,每行以行号开头,如“1”,“2”等。我不想要标题。有没有办法做到这一点?

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.

现在我甚至无法使用read.table(“outfile”,header = TRUE,sep =“\ t”)将文件再次加载到R中,它甚至与我之前输出的内容不同。 R识别文件中的第一个colunm,它们是行号作为新列。

1 个解决方案

#1


35  

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,

要在将表输出到文本文件时删除行名和列名(标题),在写入矩阵时为row.names和col.names分配FALSE,

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

#1


35  

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,

要在将表输出到文本文件时删除行名和列名(标题),在写入矩阵时为row.names和col.names分配FALSE,

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