Link to Databases (dplyr)
Let’s access an SQL database directly from R using dplyr
.
Either use an existing copy of the portal_mammals.sqlite
database or download
a new copy. You
should be able to link to the surveys
table in the database using:
library(DBI)
portaldb <- dbConnect(RSQLite::SQLite(), "portal_mammals.sqlite")
surveys <- tbl(portaldb, "surveys")
surveys
is actually a connection to the database, which means that the table
remains external to the R environment. Also, we won’t need to worry about it
printing out huge numbers of rows when we look at it.
- Select the
year
,month
,day
, andspecies_id
columns in that order. - Create a new data frame with the
year
,species_id
, and weight in kilograms of each individual, with no null weights. - Use the
distinct()
function to print thespecies_id
for each species in the dataset that has been weighed.