-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Description
Supplying a negative number to slice_head or slice_tail results in an error. Could negative numbers be allowed, so that running df %>% slice_[head/tail](n = x) is equivalent to df %>% group_modify(~ [head/tail](., x)) for both positive and negative numbers?
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
#### Remove first three rows from each group
mtcars %>%
group_by(cyl) %>%
group_modify(~ tail(.x, -3))
#> # A tibble: 23 × 11
#> # Groups: cyl [3]
#> cyl mpg disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 32.4 78.7 66 4.08 2.2 19.5 1 1 4 1
#> 2 4 30.4 75.7 52 4.93 1.62 18.5 1 1 4 2
#> 3 4 33.9 71.1 65 4.22 1.84 19.9 1 1 4 1
#> 4 4 21.5 120. 97 3.7 2.46 20.0 1 0 3 1
#> 5 4 27.3 79 66 4.08 1.94 18.9 1 1 4 1
#> 6 4 26 120. 91 4.43 2.14 16.7 0 1 5 2
#> 7 4 30.4 95.1 113 3.77 1.51 16.9 1 1 5 2
#> 8 4 21.4 121 109 4.11 2.78 18.6 1 1 4 2
#> 9 6 18.1 225 105 2.76 3.46 20.2 1 0 3 1
#> 10 6 19.2 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 13 more rows
mtcars %>%
group_by(cyl) %>%
slice_tail(n = -3)
#> Error: `n` must be a non-missing positive number.
#### Remove last three rows from each group
mtcars %>%
group_by(cyl) %>%
group_modify(~ head(.x, -3))
#> # A tibble: 23 × 11
#> # Groups: cyl [3]
#> cyl mpg disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 22.8 108 93 3.85 2.32 18.6 1 1 4 1
#> 2 4 24.4 147. 62 3.69 3.19 20 1 0 4 2
#> 3 4 22.8 141. 95 3.92 3.15 22.9 1 0 4 2
#> 4 4 32.4 78.7 66 4.08 2.2 19.5 1 1 4 1
#> 5 4 30.4 75.7 52 4.93 1.62 18.5 1 1 4 2
#> 6 4 33.9 71.1 65 4.22 1.84 19.9 1 1 4 1
#> 7 4 21.5 120. 97 3.7 2.46 20.0 1 0 3 1
#> 8 4 27.3 79 66 4.08 1.94 18.9 1 1 4 1
#> 9 6 21 160 110 3.9 2.62 16.5 0 1 4 4
#> 10 6 21 160 110 3.9 2.88 17.0 0 1 4 4
#> # … with 13 more rows
mtcars %>%
group_by(cyl) %>%
slice_head(n = -3)
#> Error: `n` must be a non-missing positive number.Created on 2021-07-28 by the reprex package (v2.0.0)
francisbarton and grayskripko
Metadata
Metadata
Assignees
Labels
No labels