Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing files from PostgreSQL to R

Tags:

r

postgresql

I have a large data-set and I will preform some analysis in R software. While I could not import the data properly to R.

I get this error:

Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect User@local on dbname "Intel"

I have used PostgreSQL to open data and somehow manage it. How can I import the existing data in the PostgreSQL to the R software?

like image 461
A.Amidi Avatar asked Sep 19 '12 08:09

A.Amidi


2 Answers

drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='localhost', port='5432', dbname='Swiss',
                 user='postgres', password='123456')

Moreover, "RPostgreSQL" package in R should be installed.

like image 98
A.Amidi Avatar answered Oct 17 '22 02:10

A.Amidi


Try the R package RPostgreSQL http://cran.r-project.org/web/packages/RPostgreSQL/ . You can see how to use it in http://code.google.com/p/rpostgresql/ . Example:

library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")   ## loads the PostgreSQL driver
con <- dbConnect(drv, dbname="R_Project")   ## Open a connection 
rs <- dbSendQuery(con, "select * from R_Users")   ## Submits a statement
fetch(rs,n=-1)   ## fetch all elements from the result set
dbGetQuery(con, "select * from R_packages")   ## Submit and execute the query
dbDisconnect(con)   ## Closes the connection
dbUnloadDriver(drv)   # Frees all the resources on the driver
like image 26
lf.xiao Avatar answered Oct 17 '22 03:10

lf.xiao