tags:

views:

254

answers:

2

I've a question about using sqlSave. How does R map RODBC data in the data frame to the database table columns?

If I've a table with columns X and Y and a data frame with columns X and Y, RODBC puts X into X and Y into Y (I found out by trail-and-error). But can I explicitly tell R how to map data.frame columns to database table columns, like put A in X and B in Y.

I'm rather new to R and think the RODBC manual is a bit cryptic. Nor can I find an example on the internet.

+1  A: 

You should find the fine R manuals of great help as you start to explore R, and its help facilities are very good too.

If you start with

  help(sqlSave)

you will see the colNames argument. Supplying a vector c("A", "B") would put your first data.frame column into a table column A etc.

Dirk Eddelbuettel
What do you mean with "its help facilities"? I use http://cran.r-project.org/web/packages/RODBC/RODBC.pdf. But that document speaks about the "colnames" argument as "logical: save column names as the first row of table?", not as a vector suppling columnNAMES
waanders
Type `help(help)` in R/
Dirk Eddelbuettel
Still the "colnames" argument is a logical
waanders
A: 

I'm now doing it this way (maybe that's also what you meant):

colnames(dat) <- c("A","B")
sqlSave(channel,dat,tablename="tblTest",rownames=FALSE,append=TRUE)

It works for me. Thanks for your help.

waanders
That alters the object before saving it which, while getting the job done, is not what you asked for.
Dirk Eddelbuettel
Sure, but the altered object is only temporarily, so it's no problem
waanders