[R] RSQLite to input dataframe

Andreas Borg
Tue, 04 Jan 2011 12:37:29 -0000

Hi Amy,
> Suppose I have a dataframe as given below.
>
> DF = data.frame(X = c("US", "UK", "Canada",
"Australia", "Newzealand"), Y = c(52, 36, 74, 10, 98))
>
> drv <- dbDriver("SQLite")
> tfile <- tempfile()
> con <- dbConnect(drv, dbname = tfile)
> data(DF)
> dbWriteTable(con, ......., .......) # Didn't know what to write here.
>

data() loads data sets which ship with R or an extension package. Your
data frame DF is already in the working environment, hence it is neither
possible nor necessary to "load" it, the expression

data(DF)

is an error. The command to write DF to a database would be something like

dbWriteTable(con, "DF", DF)

where the second argument is the name of the database table to write to, the
third argument the data frame you want to write (please have a look at the
documentation).

> I understand I have raised a query in a stupid manner. I need to understand
is there any way I can use SQLite to read
>  dataframe or for that matter any csv file say e.g. 'DF.csv'.

To copy a CSV file to SQLite, read it via read.table() or read.csv() first, then
copy the result with dbWriteTable. There might be a way to read files directly
from SQLite, but I don't know about that.

Best regards,

Andreas




>
>


--
Andreas Borg
Medizinische Informatik

UNIVERSIT?TSMEDIZIN
der Johannes Gutenberg-Universit?t
Institut f?r Medizinische Biometrie, Epidemiologie und Informatik
Obere Zahlbacher Stra?e 69, 55131 Mainz
www.imbei.uni-mainz.de

Telefon +49 (0) 6131 175062
E-Mail: borg at imbei.uni-mainz.de

Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen.
Wenn Sie nicht der
richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren
Sie bitte sofort den
Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe
dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.