Search code examples
rwrite.table

Trouble with extra character while using 'write.table'


I am using R 3.3.4 and RStudio 1.1.453. I am creating lines of data passed from a function call, then using write.table() to write one line (and only one line) of data into one file at a time to obtain a .tsv.

I know it's a noobie question but so am i. :)

for (i in 1:n) {
write.table(make_line(),
            file = (paste("file", i, ".tsv", sep = "")),
            quote = FALSE,
            eol = "\r\n",
            sep = '\t'
            )
}

However my output is:

x
1   >chr4   820383   966802   CAC   TRUE

when make_line() generates below and that is all I want:

>chr4   820383   966802   CAC   TRUE

Solution

  • Those are default row and column names. To avoid them, set row.names and col.names to FALSE in write.table.

    write.table("AAAA")
    # "x"
    # "1" "AAAA"
    
    write.table("AAAA", row.names=FALSE, col.names=FALSE)
    # "AAAA"