Using SQL to query data frames via the sqldf package
In the previous chapter, you learned how to compose SQL statements to query data from relational databases such as SQLite and MySQL. Is there a way to directly use SQL to query data frames in R as if these data frames are tables in relational databases? The sqldf
package says yes.
This package takes advantage of SQLite, thanks to its lightweight structure and easiness to embed into an R session. Run the following command to install this package if you don't have it:
install.packages("sqldf")
First, let's attach the package, as shown in the following code:
library(sqldf) ## Loading required package: gsubfn ## Loading required package: proto ## Loading required package: RSQLite ## Loading required ...
Get Learning R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.