Using sqldf and RPostgreSQL for data manipulation in R

In R, I prefer to use the sqldf library to manipulate data.  Sqldf allows me to use SQL syntax to directly query and manipulate dataframes.  It works by connecting to RDBMS of your choosing and using that to manipulate your dataframes.

I personally like to use PostgreSQL with sqldf but you can use many others depending on what you are used to(mysql, sqlite, etc.)

Below are instructions on how to configure sqldf using PostgreSQL:

1. Install PostgreSQL and setup on your computer.
2. Install RPostgreSQL and sqldf packages in R

install.packages("RPostgreSQL")
install.packages("sqldf")


3. Now you can directly query data frames in R!  Post in the comments if you run into any issues!

library(RPostgreSQL)
library(sqldf)

options(sqldf.driver = "RPostgreSQL")
options(sqldf.RPostgreSQL.user = "postgres",
sqldf.RPostgreSQL.password = <your password here>,
sqldf.RPostgreSQL.dbname = "postgres",
sqldf.RPostgreSQL.hostname = "localhost",
sqldf.RPostgreSQL.port = 5432)

sqldf("select * from mydataframe")

Comments

Popular posts from this blog

grandmaster level chess AI using python - Part 2 (the code)

building a chess ai - part 4: learning an evaluation function using deep learning (keras)

Brief intro to recurrent neural networks