library(DBI)
library(tidyverse)ch_21_solutions
Prerequisites:
con <- DBI::dbConnect(duckdb::duckdb())
dbplyr::copy_nycflights13(con)flights <- tbl(con, "flights")21.5.10 Exercises:
The equivalent of R’s
distinctfunction is the worddistinctin the select statement. The equivalent of theheadfunction for duckdb is LIMIT, but the syntax varies depending on the SQL version used.flights |> distinct(tailnum) |> show_query() ## <SQL> ## SELECT DISTINCT tailnum ## FROM flights flights |> head(5) |> show_query() ## <SQL> ## SELECT flights.* ## FROM flights ## LIMIT 5The first query filters for rows where
dep_delayis less thanarr_delay, this can be thought of as flights that gained time after departing. The second query creates a column for miles per hour (since air_time is in minutes we need to divide by 60).flights |> filter(dep_delay < arr_delay) |> show_query() ## <SQL> ## SELECT flights.* ## FROM flights ## WHERE (dep_delay < arr_delay) flights |> mutate(speed = distance / (air_time / 60)) |> show_query() ## <SQL> ## SELECT flights.*, distance / (air_time / 60.0) AS speed ## FROM flights