Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...