使用write.table时,请避免在列名和行名中使用引号 [英] Avoid quotation marks in column and row names when using write.table

查看:849
本文介绍了使用write.table时,请避免在列名和行名中使用引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 data.txt的文件中包含以下数据:

I have the following data in a file called "data.txt":

pid      1     2     4     15      18       20
1_at   100   200   89    189     299      788
2_at     8    78   33     89      90       99
3_xt   300    45   53    234      89       34
4_dx    49    34   88      8       9       15

数据由制表符分隔。

现在我想提取一些数据根据称为 vector.csv的csv文件的信息,该向量上的列包含以下数据:

Now I wanted to extract some columns on that table, based on the information of csv file called "vector.csv", this vector got the following data:

18,1,4,20

所以我想以修改后的文件 datamod.txt结尾用制表符分隔:

So I wanted to end with a modified file "datamod.txt" separated with tabs that would be:

pid      18       1     4      20
1_at   299     100    89     788
2_at    90       8    33      99
3_xt    89     300    53      34
4_dx     9      49    88      15

在一些帮助下,以下代码:

I have made, with some help, the following code:

fileName="vector.csv"
con=file(fileName,open="r")
controlfile<-readLines(con)
controls<-controlfile[1]
controlins<-controlfile[2]
test<-paste("pid",controlins,sep=",")
test2<-c(strsplit(test,","))
test3<-c(do.call("rbind",test2)) 
df<-read.table("data.txt",header=T,check.names=F)
CC <- sapply(df, class)
CC[!names(CC) %in% test3] <- "NULL"
df <- read.table("data.txt", header=T, colClasses=CC,check.names=F)
df<-df[,test3]
write.table(df,"datamod.txt",row.names=FALSE,sep="\t")

我得到的问题是我生成的文件具有以下格式:

The problem that I got is that my resulting file has the following format:

"pid"      "18"       "1"     "4"      "20"
"1_at"   299         100      89       788
"2_at"    90           8      33        99
"3_xt"    89         300      53        34
"4_dx"     9          49      88        15

我的问题是如何避免出现在保存的文件中的引号引起数据看起来像我想要的。

The question I have is how to avoid those quotation "" marks that appear in my saved file, so that the data appears like I would like to.

有帮助吗?

谢谢

推荐答案

从帮助文件中的 quote 获取 write.table

quote


值(真或假)或数字向量。如果为TRUE,则
任何字符或因子列均将双引号引起来。
如果是数字矢量,则将其元素用作要引用的
列的索引。在这两种情况下,如果将行名和列名都写成
,则将其引起引用。如果为FALSE,则不会引用任何内容。

a logical value (TRUE or FALSE) or a numeric vector. If TRUE, any character or factor columns will be surrounded by double quotes. If a numeric vector, its elements are taken as the indices of columns to quote. In both cases, row and column names are quoted if they are written. If FALSE, nothing is quoted.

因此

write.table(df,"datamod.txt",row.names=FALSE,sep="\t", quote = FALSE)

应该很好用。

这篇关于使用write.table时,请避免在列名和行名中使用引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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