tidyverse / dtplyr

Data table backend for dplyr
https://dtplyr.tidyverse.org
Other
670 stars 57 forks source link

Allow `pivot_longer()` to work on unbalanced data #467

Open markfairbanks opened 9 months ago

markfairbanks commented 9 months ago

This didn't work previously, but now it does

library(dtplyr)
library(dplyr)
library(tidyr)
library(data.table)

df <- tibble(x_5 = 5, x_6 = 6, y_7 = 7, y_8 = 8)

df %>%
  pivot_longer(everything(), names_to = c(".value", "id"), names_sep = "_")
#> # A tibble: 4 × 3
#>   id        x     y
#>   <chr> <dbl> <dbl>
#> 1 5         5    NA
#> 2 6         6    NA
#> 3 7        NA     7
#> 4 8        NA     8

data.table(df) %>%
  melt(measure.vars=measure(value.name, id, sep="_"))
#>        id     x     y
#>    <char> <num> <num>
#> 1:      5     5    NA
#> 2:      6     6    NA
#> 3:      7    NA     7
#> 4:      8    NA     8

data.table(df) %>%
  melt(measure.vars=measurev(list(value.name=NULL, id=NULL), sep="_"))
#>        id     x     y
#>    <char> <num> <num>
#> 1:      5     5    NA
#> 2:      6     6    NA
#> 3:      7    NA     7
#> 4:      8    NA     8

df %>%
  lazy_dt() %>%
  pivot_longer(everything(), names_to = c(".value", "id"), names_sep = "_") %>%
  collect()
#> Error in `pivot_longer()`:
#> ! `data.table::melt()` doesn't currently support melting of unbalanced datasets.