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.
To write DataFrame to CSV without column header (remove column names) use header=False param on to_csv() method.
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.
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.
Just simply put header=False and for eliminating the index using index=False.
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>
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With