Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support negative n in slice_head and slice_tail #5961

Closed
eutwt opened this issue Jul 29, 2021 · 1 comment · Fixed by #5962
Closed

Support negative n in slice_head and slice_tail #5961

eutwt opened this issue Jul 29, 2021 · 1 comment · Fixed by #5962

Comments

@eutwt
Copy link
Contributor

eutwt commented Jul 29, 2021

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)

@grayskripko
Copy link

grayskripko commented Jul 12, 2022

A temporary workaround for head(n=-1)

> tibble(gr=c(1,1,2,2,2), val=1:5) %>% 
+     group_by(gr) %>% 
+     filter(n() - row_number() + 1 >**1**) %>% 
+     ungroup()
# A tibble: 3 x 2
     gr   val
  <dbl> <int>
1     1     1
2     2     3
3     2     4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants