In the following example, I am passing in a grouped dataframe to slice_head.
suppressWarnings(library(dplyr))
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- tibble(
group = rep(c("b", "c", "a"), c(1, 2, 4)),
x = runif(7)
)
df
#> # A tibble: 7 x 2
#> group x
#> <chr> <dbl>
#> 1 b 0.648
#> 2 c 0.247
#> 3 c 0.192
#> 4 a 0.0977
#> 5 a 0.0479
#> 6 a 0.216
#> 7 a 0.501
df %>%
group_by(group) %>%
slice_head(n = 1)
#> # A tibble: 3 x 2
#> # Groups: group [3]
#> group x
#> <chr> <dbl>
#> 1 a 0.0977
#> 2 b 0.648
#> 3 c 0.247
In my case (and it seems like this is the intuitive result) I would like the see the grouping order retained, but it alphanumerically sorts by the grouping variable after chopping the head off of each group. From the documentation, it appears that this is exactly what the .preserve argument in slice is meant to address, however when I use slice and set .preserve = TRUE I get the exact same output.
df %>%
group_by(group) %>%
slice(1, .preserve = TRUE)
#> # A tibble: 3 x 2
#> # Groups: group [3]
#> group x
#> <chr> <dbl>
#> 1 a 0.958
#> 2 b 0.680
#> 3 c 0.927
The following is the source code for slice_head
function (.data, ..., n, prop)
{
ellipsis::check_dots_empty()
size <- check_slice_size(n, prop)
idx <- switch(size$type, n = function(n) seq2(1, min(size$n,
n)), prop = function(n) seq2(1, min(size$prop * n, n)))
slice(.data, idx(dplyr::n()))
}
It appears that slice_head is calling slice which, by default, has .preserve = FALSE. It seems that, at the least, slice_head should allow the user to determine the value of .preserve, however that still leaves me wondering why slice with .preserve = TRUE didn't seem to work, as illustrated above. Am I doing something stupid here? I know there are easy workarounds but this is just bugging me.