4
4
# '
5
5
# ' @description
6
6
# ' These are methods for the dplyr [slice()], `slice_head()`, `slice_tail()`,
7
- # ' `slice_min()`, `slice_max()` and `slice_sample()` generics. `slice()`
8
- # ' and `slice_sample()` are translated to the `i` argument of `[.data.table`,
9
- # ' all others are translated to the `j` argument.
7
+ # ' `slice_min()`, `slice_max()` and `slice_sample()` generics. They are
8
+ # ' translated to the `i` argument of `[.data.table`.
10
9
# '
11
10
# ' Unlike dplyr, `slice()` (and `slice()` alone) returns the same number of
12
11
# ' rows per group, regardless of whether or not the indices appear in each
@@ -80,11 +79,11 @@ slice.data.table <- function(.data, ...) {
80
79
slice_head.dtplyr_step <- function (.data , ... , n , prop ) {
81
80
ellipsis :: check_dots_empty()
82
81
size <- check_slice_size(n , prop )
83
- j <- switch (size $ type ,
84
- n = expr(head( .SD , !! size $ n )),
85
- prop = expr(head( .SD , !! size $ prop * .N )),
82
+ i <- switch (size $ type ,
83
+ n = expr(seq.int(min( !! size $ n , .N ) )),
84
+ prop = expr(seq.int( !! size $ prop * .N )),
86
85
)
87
- step_subset_j (.data , j = j )
86
+ step_subset_i (.data , i = i )
88
87
}
89
88
90
89
# ' @rdname slice.dtplyr_step
@@ -93,11 +92,11 @@ slice_head.dtplyr_step <- function(.data, ..., n, prop) {
93
92
slice_tail.dtplyr_step <- function (.data , ... , n , prop ) {
94
93
ellipsis :: check_dots_empty()
95
94
size <- check_slice_size(n , prop )
96
- j <- switch (size $ type ,
97
- n = expr(tail( .SD , !! size $ n )),
98
- prop = expr(tail( .SD , floor( !! size $ prop * .N )) ),
95
+ n_sequence <- switch (size $ type ,
96
+ n = expr(min( !! size $ n , .N )),
97
+ prop = expr(!! size $ prop * .N ),
99
98
)
100
- step_subset_j (.data , j = j )
99
+ step_subset_i (.data , i = expr(seq.int( .N - !! n_sequence + 1 , .N )) )
101
100
}
102
101
103
102
# ' @rdname slice.dtplyr_step
0 commit comments