Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export proper TSV?

Tags:

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.

like image 898
Konrad Rudolph Avatar asked Jun 14 '13 12:06

Konrad Rudolph


People also ask

How do I save a CSV file as a TSV?

From the File menu, select Download as then select Tab-separated values (. tsv, current sheet). This will download the currently selected sheet as a tab-separated . TSV file, inheriting the name of your spreadsheet.

What is the difference between CSV and TSV?

CSV uses an escape syntax to represent comma and newlines in the data. TSV takes a different approach, disallowing TABs and newlines in the data. The escape syntax enables CSV to fully represent common written text. This is a good fit for human edited documents, notably spreadsheets.


1 Answers

You can use col.names = NA:

write.table(test, file='test.tsv', quote=FALSE, sep='\t', col.names = NA)
like image 114
alexwhan Avatar answered Jan 21 '23 12:01

alexwhan