How do I get rid of quotes in column and row labels in R?

I have the following data in a file named "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 in this table based on information of a csv file named "vector.csv", this vector got the following data:

18,1,4,20

      

So, I wanted to end up with a modified "datamod.txt" file separated by tabs that will 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

      

With some help, I made 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 I got is that my final file is in 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

      

I have a question how to avoid those quotes appearing in my saved file so that the data looks as I would like.

Any help?

thank

+3


source to share


1 answer


To specify from the help file for write.table

quote

a boolean value (TRUE or FALSE) or a numeric vector. If true, any columns of characters or factors will be surrounded by double quotes. If a numeric vector, its elements are taken as column indexes Quote. In both cases, the row and column names are quoted, if written. If FALSE, nothing is quoted.



therefore

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

      

should work well.

+13


source







All Articles