Hi,
I have a method to create a list column of a categorical variable grouped by an id variable using dplyr. However this does not work in dbplyr. Does anyone know of a way to do this in dbplyr??
Thanks
David
library(tidyverse)
library(dplyr)
library(nycflights13)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
copy_to(con, flights)
flights2 <- tbl(con, "flights")
flights |>
head(100) |>
select(carrier, flight) |>
nest_by(carrier)
#> # A tibble: 11 × 2
#> # Rowwise: carrier
#> carrier data
#> <chr> <list<tibble[,1]>>
#> 1 AA [17 × 1]
#> 2 AS [1 × 1]
#> 3 B6 [25 × 1]
#> 4 DL [13 × 1]
#> 5 EV [3 × 1]
#> 6 FL [1 × 1]
#> 7 MQ [6 × 1]
#> 8 UA [26 × 1]
#> 9 US [5 × 1]
#> 10 VX [2 × 1]
#> 11 WN [1 × 1]
flights2 |>
head(100) |>
select(carrier, flight) |>
nest_by(carrier)
#> Error in UseMethod("nest_by"): no applicable method for 'nest_by' applied to an object of class "c('tbl_SQLiteConnection', 'tbl_dbi', 'tbl_sql', 'tbl_lazy', 'tbl')"
Short answer: the documentation is misleading—it says that nest_by will work a lazy data frame in the arguments section, but takes it back in methods
The following methods are currently available in loaded packages: dplyr (data.frame , grouped_df )
which sort of makes sense if you remember that the mission of {dplyr} is to translate tidy-style data statements into SQL queries. Once you have results from the con, continue by converting to a data frame.
library(dbplyr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:dbplyr':
#>
#> ident, sql
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(nycflights13)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
copy_to(con, flights)
flights2 <- tbl(con, "flights")
as.data.frame(flights2) |>
head(100) |>
select(carrier, flight) |>
nest_by(carrier)
#> # A tibble: 11 × 2
#> # Rowwise: carrier
#> carrier data
#> <chr> <list<tibble[,1]>>
#> 1 AA [17 × 1]
#> 2 AS [1 × 1]
#> 3 B6 [25 × 1]
#> 4 DL [13 × 1]
#> 5 EV [3 × 1]
#> 6 FL [1 × 1]
#> 7 MQ [6 × 1]
#> 8 UA [26 × 1]
#> 9 US [5 × 1]
#> 10 VX [2 × 1]
#> 11 WN [1 × 1]