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
#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!