Using SQL in R

Thanks to the sqldf package, using SQL in R to analyze & transform data is seamless. First, install & load the package:

#install
install.packages("sqldf")

#load
library(sqldf)

This package allows you to use the SQL programming language on your current R datasets. You can create new datasets, transform existing ones, incorporate functions…the possibilities are endless. Here is the documentation guide for a complete rundown of the package & list of supported SQL versions. There are so many awesome ways to incorporate SQL statements into your R code. Simply wrap your SELECT statement in double quotation marks & sqldf will do the rest. 

Example

Below are two basic examples to give the gist, but there are more detailed articles on our SQL page with more elaborate strategies to completely transform your R code. 

#create example data:
ex_data <- tibble(id = c(1, 2, 3, 4),
                  license_num = c(65, 76, 09, 29),
                  month = c('1', '8', '3', '11'))

#example 1 - Isolate rows where id > 2
ex1 <- sqldf("SELECT * FROM ex_data WHERE id > 2")

#example 2 - How many ID's are in the dataset?
ex2 <- sqldf("SELECT count(id) AS id_count FROM ex_data")

Check out the related posts below for more coding tips!

Leave a Comment

Your email address will not be published. Required fields are marked *