Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

print matrix in R to a file

I am using R to select the first 2 columns from a file and using a function "table" to make a matrix from it then print it to a file. the problem is that the header is shifted to the left by 1 cell.

The input file is:

ExoT    ID3 99.64   1374    5   0   1   1374    15428   16801   0.0 2510
ExoT    ID2 99.64   1374    5   0   1   1374    11168   12541   0.0 2510
ExoT    ID1 99.64   1374    5   0   1   1374    11942   13315   0.0 2510
ExoU    ID3 100.00  2064    0   0   1   2064    1144684 1146747 0.0 3812
ExoU    ID2 100.00  2064    0   0   1   2064    1245564 1247627 0.0 3812
ExoU    ID1 100.00  2064    0   0   1   2064    1156352 1158415 0.0 3812
ExoS    ID1 100.00  2064    0   0   1   2064    1156352 1158415 0.0 3812

The desired output is:

        ID1 ID2 ID3
  ExoS   1   0   0
  ExoT   1   1   1
  ExoU   1   1   1

The actual output is:

  ID1 ID2 ID3
  ExoS   1   0   0
  ExoT   1   1   1
  ExoU   1   1   1

It seems that a tabb is missing in the begining!

my code is:

args <- commandArgs(TRUE)
blast_file <- read.table(args[1])
selected <- subset(blast_file, select = c(V1, V2))
table (selected)
final <- table (selected)
write.table(final,file=args[2],sep="	")

Any Ideas??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To keep your row.names and col.names and have a tabb at first, you can export your file in two steps :

first :

write.table(c("",colnames(final)),file=args[2],sep="	")

then :

write.table(final,file=args[2],sep="	",col.names=F,append=T,row.names=T)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...