Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export CSV without col.names

Tags:

sqlite

r

csv

I need to take a data.frame and export it to a CSV file (or something else, but CSV seemed like the easiest well-formed format) so I can import it into an SQLite database.

However, it looks like write.csv() requires that I write a header line, and SQLite's .import command requires that I don't have a header line. So that's a bit of a mismatch.

Here's what happens if I try to omit the header line:

> write.csv(mydf, "/tmp/mydf.csv", row.names=F, col.names=F) Warning message: In write.csv(mydf, "/tmp/mydf.csv", row.names = F, col.names = F) :   attempt to set 'col.names' ignored 

I have to wonder why it's enforcing that in the first place - the manual says "These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning." But I know of nothing in the spec or elsewhere requiring column names - indeed, most tools (Excel, etc.) don't treat them specially.

like image 447
Ken Williams Avatar asked Jul 19 '11 16:07

Ken Williams


People also ask

How do I save a CSV file without headers?

To write DataFrame to CSV without column header (remove column names) use header=False param on to_csv() method.

How do I remove my name from Col?

To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified.

Do CSV files always have headers?

The CSV format is not standardized, so various implementations exist. In 2005, the Internet Society published guidelines for creating CSV files. They wrote down best practices to structure and process CSV data. From those guidelines and giving the lack of standardization, the header line is optional in a CSV file.

How do I remove the index and header from a data frame?

Just simply put header=False and for eliminating the index using index=False.


2 Answers

If you can't beat 'em, join 'em.

If you switch to write.table() (which write.csv() calls anyway) you're golden:

R> write.table(trees, file="/tmp/trees.csv",  +              row.names=FALSE, col.names=FALSE, sep=",") R> system("head /tmp/trees.csv") 8.3,70,10.3 8.6,65,10.3 8.8,63,10.2 10.5,72,16.4 10.7,81,18.8 10.8,83,19.7 11,66,15.6 11,75,18.2 11.1,80,22.6 11.2,75,19.9 R> 
like image 50
Dirk Eddelbuettel Avatar answered Sep 17 '22 19:09

Dirk Eddelbuettel


You can directly import it into SQLite. The following imports the built in data frame BOD into the SQLite database my.db (creating my.db if it does not already exist).

library(RSQLite) con <- dbConnect(SQLite(), dbname = "my.db") dbWriteTable(con, "BOD", BOD, row.names = FALSE) dbDisconnect(con) 
like image 22
G. Grothendieck Avatar answered Sep 18 '22 19:09

G. Grothendieck