Export CSV without col.names
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.
Solution 1:
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>
Solution 2:
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)