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 package: DBI
Note that when we attach sqldf
, a number of other packages are automatically loaded. The sqldf
package depends on these packages, because what it does is basically transferring data and converting data...