将R中的矩阵打印到文件中 [英] print matrix in R to a file

查看:159
本文介绍了将R中的矩阵打印到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R从文件中选择前两列,并使用函数表"从中创建矩阵,然后将其打印到文件中.问题是页眉向左移动了1个单元格.

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.

输入文件为:

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

所需的输出是:

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

实际输出为:

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

一开始似乎没有Tabb!

It seems that a tabb is missing in the begining!

我的代码是:

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="\t")

任何想法?

推荐答案

要保留row.namescol.names并首先具有tabb,可以分两步导出文件:

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

第一:

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

然后:

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

这篇关于将R中的矩阵打印到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆