Remember to
- download
portal_mammals.sqlite.- open
portal_mammals.sqlitein SQLite Manager.- make sure the copy you are going to use in class does not have the
SpeciesCountstable or view.
- We’ve already seen briefly how to work with databases using
dplyr. - Let’s get more familiar with the
dplyrsoftware for databases. dplyruses ‘low-level’ database management libraries (DBI,RSQLite) for powerful and cohesive interaction with databases.
Connect
library(dplyr)
portaldb <- src_sqlite("portal_mammals.sqlite")
Check out database structure
portaldb
tbl(portaldb, "plots")
surveys <- tbl(portaldb, "surveys") %>% collect()
surveys
surveys$ops$vars
Run queries review
query <- "SELECT genus, species, COUNT(*)
FROM surveys JOIN species
ON surveys.species_id = species.species_id
GROUP BY genus, species"
species_counts <- tbl(portaldb, sql(query))
Write new information to database
Show the original
portal_mammals.sqlitein SQLite Manager.
copy_to(portaldb, species_counts)
Show
species_countstable NOT inportal_mammals.sqlite.
copy_to(portaldb, species_counts, temporary=FALSE,
name="SpeciesCounts")
Show
SpeciesCountstable inportal_mammals.sqlitewith new name.
Assign remaining exercises.
