Question

Short and sweet: how can I export TSV/CSV from R?

write.table / write.csv almost works:

test <- data.frame(a = 2 : 4, b = 3 : 5)
write.table(test, file='test.tsv', quote=FALSE, sep='\t')
$ more test.tsv
a   b
1   2   3
2   3   4
3   4   5

… but produces a format that is different from what’s expected by most other programs:

    a   b
1   2   3
2   3   4
3   4   5

– note the different handling of the header row.

How can I export the second rather than the first format? Manually specifying the col.names as c('', colnames(test)) doesn’t work – R complains about an invalid argument.

Was it helpful?

Solution

You can use col.names = NA:

write.table(test, file='test.tsv', quote=FALSE, sep='\t', col.names = NA)

OTHER TIPS

you could also consider excluding the row names row.names = FALSE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top