Avoid quotation marks in column and row names when using write.table [duplicate]
Asked Answered
T

1

16

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

The data is separated by tabs.

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

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.

Any help?

Thanks

Tarmac answered 13/2, 2013 at 4:33 Comment(1)
Have a look at the quote argument to write.table set it too FALSESelfdelusion
S
61

To quote from the help file for write.table

quote

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.

Therefore

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

should work nicely.

Selfdelusion answered 13/2, 2013 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.