Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 295
Layla Avatar asked Feb 13 '13 04:02

Layla


People also ask

How do I write a csv file without quotes?

If you want to write a data frame as a csv file without quoting values and strings then you can set the quote=FALSE argument when calling write. table or one if its wrappers such as write. csv and write. csv2 .

How do you remove double quotes in output?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

What is the function of double quotation marks when using a string?

Quotation marks are used to specify a literal string. You can enclose a string in single quotation marks ( ' ) or double quotation marks ( " ). Quotation marks are also used to create a here-string. A here-string is a single-quoted or double-quoted string in which quotation marks are interpreted literally.


1 Answers

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.

like image 118
mnel Avatar answered Sep 24 '22 05:09

mnel