markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
449 stars 33 forks source link

slice head returns one row when 0 rows data frame #642

Closed jfdesomzee closed 1 year ago

jfdesomzee commented 1 year ago

Hello,

I have another issue with 0 rows data set (sorry to bother with specific cases). When slicing head of 0 rows data I would expect to get 0 rows and not 1.

require(tidytable)
#> Loading required package: tidytable
#> As of tidytable v0.9.0 dotless versions of functions are exported.
#> You can now use `arrange()`/`mutate()`/etc. directly.
#> 
#> Attaching package: 'tidytable'
#> The following objects are masked from 'package:stats':
#> 
#>     dt, filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     %in%, ifelse
iris %>% 
  as_tidytable() %>% 
  filter(FALSE) %>% 
  slice_head.(2)
#> # A tidytable: 1 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#> 1           NA          NA           NA          NA <NA>
markfairbanks commented 1 year ago

I have another issue with 0 rows data set (sorry to bother with specific cases).

No worries, thanks for catching these. It's very helpful when you report them 😄

markfairbanks commented 1 year ago

Note (mostly to myself): this also occurs with slice_tail()

pacman::p_load(tidytable)

df <- tidytable(x = integer(), y = character())

df %>%
  slice_tail(2)
#> # A tidytable: 1 × 2
#>       x y    
#>   <int> <chr>
#> 1    NA <NA>
markfairbanks commented 1 year ago

All set.

pacman::p_load(tidytable)

df <- tidytable(x = integer(), y = character())

df %>%
  slice_head(2)
#> # A tidytable: 0 × 2
#> # … with 2 variables: x <int>, y <chr>

df %>%
  slice_tail(2)
#> # A tidytable: 0 × 2
#> # … with 2 variables: x <int>, y <chr>