R/slice-head-tail.R
, R/slice-min-max.R
, R/slice.R
, and 1 more
slice.Rd
Choose rows in a data.table. Grouped data.tables grab rows within each group.
slice_head(.df, n = 5, ..., .by = NULL, by = NULL)
slice_tail(.df, n = 5, ..., .by = NULL, by = NULL)
slice_max(.df, order_by, n = 1, ..., with_ties = TRUE, .by = NULL, by = NULL)
slice_min(.df, order_by, n = 1, ..., with_ties = TRUE, .by = NULL, by = NULL)
slice(.df, ..., .by = NULL)
slice_sample(
.df,
n,
prop,
weight_by = NULL,
replace = FALSE,
.by = NULL,
by = NULL
)
A data.frame or data.table
Number of rows to grab
Integer row values
Columns to group by
Variable to arrange by
Should ties be kept together. The default TRUE
may return
can return multiple rows if they are equal. Use FALSE
to ignore ties.
The proportion of rows to select
Sampling weights
Should sampling be performed with (TRUE
) or without (FALSE
, default) replacement
df <- data.table(
x = 1:4,
y = 5:8,
z = c("a", "a", "a", "b")
)
df %>%
slice(1:3)
#> # A tidytable: 3 × 3
#> x y z
#> <int> <int> <chr>
#> 1 1 5 a
#> 2 2 6 a
#> 3 3 7 a
df %>%
slice(1, 3)
#> # A tidytable: 2 × 3
#> x y z
#> <int> <int> <chr>
#> 1 1 5 a
#> 2 3 7 a
df %>%
slice(1:2, .by = z)
#> # A tidytable: 3 × 3
#> x y z
#> <int> <int> <chr>
#> 1 1 5 a
#> 2 2 6 a
#> 3 4 8 b
df %>%
slice_head(1, .by = z)
#> # A tidytable: 2 × 3
#> x y z
#> <int> <int> <chr>
#> 1 1 5 a
#> 2 4 8 b
df %>%
slice_tail(1, .by = z)
#> # A tidytable: 2 × 3
#> x y z
#> <int> <int> <chr>
#> 1 3 7 a
#> 2 4 8 b
df %>%
slice_max(order_by = x, .by = z)
#> # A tidytable: 2 × 3
#> x y z
#> <int> <int> <chr>
#> 1 4 8 b
#> 2 3 7 a
df %>%
slice_min(order_by = y, .by = z)
#> # A tidytable: 2 × 3
#> x y z
#> <int> <int> <chr>
#> 1 1 5 a
#> 2 4 8 b