T. BruceLee T. BruceLee - 26 days ago 7
R Question

Generate a text file without apostrophe around my cells containing data

I have a dataframe similar to the following example:

structure(list(`t-001` = c("19790101", "19.25,11.09", "21.02,10.77",
"21.02,10.21", "20.31,9.94", "20.84,9.9"), `t-002` = c("19790101",
"21.19,11.08", "22.53,12.22", "22.64,11.64", "22.42,11.49", "22.01,10.74"
)), .Names = c("t-001", "t-002"), row.names = c(NA, 6L), class = "data.frame")

t-001 t-002
1 19790101 19790101
2 19.25,11.09 21.19,11.08
3 21.02,10.77 22.53,12.22
4 21.02,10.21 22.64,11.64
5 20.31,9.94 22.42,11.49
6 20.84,9.9 22.01,10.74


What I would like to see when I generate a text-file is, with t-001 as text-file name:

19790101
19.25,11.09
21.02,10.77
21.02,10.21
20.31,9.94
20.84,9.9


However what I get is:

"19790101"
"19.25,11.09"
"21.02,10.77"
"21.02,10.21"
"20.31,9.94"
"20.84,9.9"


I use the following loop to generate my text-files:

# loop writing text files, for 32 columns, without column and row names, and use the columnnames as text file names.

for(i in c(1:32)){
write.table(df[,i],row.names = FALSE, col.names = FALSE,file=paste0(names(df)[i],".txt"))
}


is there a way to remove the apostrophes " " in my textfile?

Oh, an important note:

I use the same loop to also write similar text files, and this does not generate an apostrophe in the textfile. So it must have something to do with the comma separated data in one column I guess.... because that is the only difference.

I hope I provided enough information, and clarity on my question. thanks

Answer

I think just setting quotes=FALSE in your call to write.table should fix this:

write.table(df.t.max8[,i],
            row.names = FALSE,
            col.names = FALSE,
            file=paste0(names(df.t.max8)[i],".txt"),
            quote = FALSE)

.....