nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
338 stars 15 forks source link

nested_by() fails with one single column in 'data' #124

Closed TiagoOlivoto closed 7 months ago

TiagoOlivoto commented 7 months ago

Hi, Thanks for the package! I faced a strange behavior when nesting a data frame, where an error returns only if a single column is used in the nested data. See the following reprex

library(poorman)
library(dplyr)

# works
mtcars |> 
  poorman::select(mpg, cyl, am, drat) |> 
  poorman::nest_by(mpg, cyl)
#>     mpg cyl                   data
#> 1  10.4   8             1.00, 4.11
#> 2  13.3   8               0.0, 3.7
#> 3  14.3   8 1.00, 0.00, 3.85, 3.92
#> 4  14.7   8             0.00, 3.69
#> 5  15.0   8             1.00, 4.43
#> 6  15.2   8             1.00, 4.08
#> 7  15.5   8 1.00, 1.00, 4.93, 3.77
#> 8  15.8   8             1.00, 4.08
#> 9  16.4   8             1.00, 4.22
#> 10 17.3   8             0.00, 3.92
#> 11 17.8   6             0.00, 2.76
#> 12 18.1   6             0.00, 3.92
#> 13 18.7   8             1.00, 3.62
#> 14 19.2   6     1.0, 1.0, 3.9, 3.9
#> 15 19.2   8             0.00, 3.08
#> 16 19.7   6 0.00, 0.00, 2.93, 3.00
#> 17 21.0   6             0.00, 3.73
#> 18 21.4   4             0.00, 3.21
#> 19 21.4   6             0.00, 3.23
#> 20 21.5   4             1.00, 3.54
#> 21 22.8   4 0.00, 0.00, 3.07, 3.15
#> 22 24.4   4             0.00, 2.76
#> 23 26.0   4             1.00, 4.22
#> 24 27.3   4             0.00, 3.07
#> 25 30.4   4             0.00, 3.07
#> 26 32.4   4             0.00, 3.15
#> 27 33.9   4             0.00, 3.08

# do not work
mtcars |> 
  poorman::select(mpg, cyl, am) |> 
  poorman::nest_by(mpg, cyl)
#> Error in `[[<-.data.frame`(`*tmp*`, .key, value = list()): substituto tem 0 linha, dados têm 27

# I was expecting this
mtcars |> 
  dplyr::select(mpg, cyl, am) |> 
  dplyr::nest_by(am, cyl)
#> # A tibble: 6 × 3
#> # Rowwise:  am, cyl
#>      am   cyl               data
#>   <dbl> <dbl> <list<tibble[,1]>>
#> 1     0     4            [3 × 1]
#> 2     0     6            [4 × 1]
#> 3     0     8           [12 × 1]
#> 4     1     4            [8 × 1]
#> 5     1     6            [3 × 1]
#> 6     1     8            [2 × 1]

Created on 2024-02-07 with reprex v2.0.2

Any ideia about this? Thanks!