`slice_dt()` lets you index rows by their (integer) locations. It allows you to select, remove, and duplicate rows. It is accompanied by a number of helpers for common use cases:

* `slice_head_dt()` and `slice_tail_dt()` select the first or last rows. * `slice_sample_dt()` randomly selects rows. * `slice_min_dt()` and `slice_max_dt()` select rows with highest or lowest values of a variable.

slice_dt(.data, ..., by = NULL)

slice_head_dt(.data, n, by = NULL)

slice_tail_dt(.data, n, by = NULL)

slice_max_dt(.data, order_by, n, by = NULL, with_ties = TRUE)

slice_min_dt(.data, order_by, n, by = NULL, with_ties = TRUE)

slice_sample_dt(.data, n, replace = FALSE, by = NULL)

Arguments

.data

A data.table

...

Provide either positive values to keep, or negative values to drop. The values provided must be either all positive or all negative.

by

Slice by which group(s)?

n

When larger than or equal to 1, the number of rows. When between 0 and 1, the proportion of rows to select.

order_by

Variable or function of variables to order by.

with_ties

Should ties be kept together? The default, `TRUE`, may return more rows than you request. Use `FALSE` to ignore ties, and return the first `n` rows.

replace

Should sampling be performed with (`TRUE`) or without (`FALSE`, the default) replacement.

Value

A data.table

See also

Examples


a = iris
slice_dt(a,1,2)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          5.1         3.5          1.4         0.2  setosa
#> 2:          4.9         3.0          1.4         0.2  setosa
slice_dt(a,2:3)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          4.9         3.0          1.4         0.2  setosa
#> 2:          4.7         3.2          1.3         0.2  setosa
slice_dt(a,141:.N)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>            <num>       <num>        <num>       <num>    <fctr>
#>  1:          6.7         3.1          5.6         2.4 virginica
#>  2:          6.9         3.1          5.1         2.3 virginica
#>  3:          5.8         2.7          5.1         1.9 virginica
#>  4:          6.8         3.2          5.9         2.3 virginica
#>  5:          6.7         3.3          5.7         2.5 virginica
#>  6:          6.7         3.0          5.2         2.3 virginica
#>  7:          6.3         2.5          5.0         1.9 virginica
#>  8:          6.5         3.0          5.2         2.0 virginica
#>  9:          6.2         3.4          5.4         2.3 virginica
#> 10:          5.9         3.0          5.1         1.8 virginica
slice_dt(a,1,.N)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>           <num>       <num>        <num>       <num>    <fctr>
#> 1:          5.1         3.5          1.4         0.2    setosa
#> 2:          5.9         3.0          5.1         1.8 virginica
slice_head_dt(a,5)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <num>       <num>        <num>       <num>  <fctr>
#> 1:          5.1         3.5          1.4         0.2  setosa
#> 2:          4.9         3.0          1.4         0.2  setosa
#> 3:          4.7         3.2          1.3         0.2  setosa
#> 4:          4.6         3.1          1.5         0.2  setosa
#> 5:          5.0         3.6          1.4         0.2  setosa
slice_head_dt(a,0.1)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>            <num>       <num>        <num>       <num>  <fctr>
#>  1:          5.1         3.5          1.4         0.2  setosa
#>  2:          4.9         3.0          1.4         0.2  setosa
#>  3:          4.7         3.2          1.3         0.2  setosa
#>  4:          4.6         3.1          1.5         0.2  setosa
#>  5:          5.0         3.6          1.4         0.2  setosa
#>  6:          5.4         3.9          1.7         0.4  setosa
#>  7:          4.6         3.4          1.4         0.3  setosa
#>  8:          5.0         3.4          1.5         0.2  setosa
#>  9:          4.4         2.9          1.4         0.2  setosa
#> 10:          4.9         3.1          1.5         0.1  setosa
#> 11:          5.4         3.7          1.5         0.2  setosa
#> 12:          4.8         3.4          1.6         0.2  setosa
#> 13:          4.8         3.0          1.4         0.1  setosa
#> 14:          4.3         3.0          1.1         0.1  setosa
#> 15:          5.8         4.0          1.2         0.2  setosa
slice_tail_dt(a,5)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>           <num>       <num>        <num>       <num>    <fctr>
#> 1:          6.7         3.0          5.2         2.3 virginica
#> 2:          6.3         2.5          5.0         1.9 virginica
#> 3:          6.5         3.0          5.2         2.0 virginica
#> 4:          6.2         3.4          5.4         2.3 virginica
#> 5:          5.9         3.0          5.1         1.8 virginica
slice_tail_dt(a,0.1)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>            <num>       <num>        <num>       <num>    <fctr>
#>  1:          7.7         3.0          6.1         2.3 virginica
#>  2:          6.3         3.4          5.6         2.4 virginica
#>  3:          6.4         3.1          5.5         1.8 virginica
#>  4:          6.0         3.0          4.8         1.8 virginica
#>  5:          6.9         3.1          5.4         2.1 virginica
#>  6:          6.7         3.1          5.6         2.4 virginica
#>  7:          6.9         3.1          5.1         2.3 virginica
#>  8:          5.8         2.7          5.1         1.9 virginica
#>  9:          6.8         3.2          5.9         2.3 virginica
#> 10:          6.7         3.3          5.7         2.5 virginica
#> 11:          6.7         3.0          5.2         2.3 virginica
#> 12:          6.3         2.5          5.0         1.9 virginica
#> 13:          6.5         3.0          5.2         2.0 virginica
#> 14:          6.2         3.4          5.4         2.3 virginica
#> 15:          5.9         3.0          5.1         1.8 virginica
slice_max_dt(a,Sepal.Length,10)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>            <num>       <num>        <num>       <num>    <fctr>
#>  1:          7.9         3.8          6.4         2.0 virginica
#>  2:          7.7         3.8          6.7         2.2 virginica
#>  3:          7.7         2.6          6.9         2.3 virginica
#>  4:          7.7         2.8          6.7         2.0 virginica
#>  5:          7.7         3.0          6.1         2.3 virginica
#>  6:          7.6         3.0          6.6         2.1 virginica
#>  7:          7.4         2.8          6.1         1.9 virginica
#>  8:          7.3         2.9          6.3         1.8 virginica
#>  9:          7.2         3.6          6.1         2.5 virginica
#> 10:          7.2         3.2          6.0         1.8 virginica
#> 11:          7.2         3.0          5.8         1.6 virginica
slice_max_dt(a,Sepal.Length,10,with_ties = FALSE)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>            <num>       <num>        <num>       <num>    <fctr>
#>  1:          7.9         3.8          6.4         2.0 virginica
#>  2:          7.7         3.8          6.7         2.2 virginica
#>  3:          7.7         2.6          6.9         2.3 virginica
#>  4:          7.7         2.8          6.7         2.0 virginica
#>  5:          7.7         3.0          6.1         2.3 virginica
#>  6:          7.6         3.0          6.6         2.1 virginica
#>  7:          7.4         2.8          6.1         1.9 virginica
#>  8:          7.3         2.9          6.3         1.8 virginica
#>  9:          7.2         3.6          6.1         2.5 virginica
#> 10:          7.2         3.2          6.0         1.8 virginica
slice_min_dt(a,Sepal.Length,10)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>            <num>       <num>        <num>       <num>  <fctr>
#>  1:          4.3         3.0          1.1         0.1  setosa
#>  2:          4.4         2.9          1.4         0.2  setosa
#>  3:          4.4         3.0          1.3         0.2  setosa
#>  4:          4.4         3.2          1.3         0.2  setosa
#>  5:          4.5         2.3          1.3         0.3  setosa
#>  6:          4.6         3.1          1.5         0.2  setosa
#>  7:          4.6         3.4          1.4         0.3  setosa
#>  8:          4.6         3.6          1.0         0.2  setosa
#>  9:          4.6         3.2          1.4         0.2  setosa
#> 10:          4.7         3.2          1.3         0.2  setosa
#> 11:          4.7         3.2          1.6         0.2  setosa
slice_min_dt(a,Sepal.Length,10,with_ties = FALSE)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>            <num>       <num>        <num>       <num>  <fctr>
#>  1:          4.3         3.0          1.1         0.1  setosa
#>  2:          4.4         2.9          1.4         0.2  setosa
#>  3:          4.4         3.0          1.3         0.2  setosa
#>  4:          4.4         3.2          1.3         0.2  setosa
#>  5:          4.5         2.3          1.3         0.3  setosa
#>  6:          4.6         3.1          1.5         0.2  setosa
#>  7:          4.6         3.4          1.4         0.3  setosa
#>  8:          4.6         3.6          1.0         0.2  setosa
#>  9:          4.6         3.2          1.4         0.2  setosa
#> 10:          4.7         3.2          1.3         0.2  setosa
slice_sample_dt(a,10)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#>            <num>       <num>        <num>       <num>     <fctr>
#>  1:          5.8         2.8          5.1         2.4  virginica
#>  2:          5.8         2.7          3.9         1.2 versicolor
#>  3:          5.1         3.8          1.5         0.3     setosa
#>  4:          5.4         3.0          4.5         1.5 versicolor
#>  5:          6.7         2.5          5.8         1.8  virginica
#>  6:          6.5         3.2          5.1         2.0  virginica
#>  7:          5.4         3.9          1.3         0.4     setosa
#>  8:          6.3         2.5          4.9         1.5 versicolor
#>  9:          5.5         4.2          1.4         0.2     setosa
#> 10:          4.6         3.2          1.4         0.2     setosa
slice_sample_dt(a,0.1)
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#>            <num>       <num>        <num>       <num>     <fctr>
#>  1:          6.7         3.0          5.0         1.7 versicolor
#>  2:          4.9         3.1          1.5         0.1     setosa
#>  3:          5.0         3.4          1.5         0.2     setosa
#>  4:          5.2         3.4          1.4         0.2     setosa
#>  5:          6.0         2.2          5.0         1.5  virginica
#>  6:          5.2         4.1          1.5         0.1     setosa
#>  7:          7.2         3.0          5.8         1.6  virginica
#>  8:          5.4         3.0          4.5         1.5 versicolor
#>  9:          5.8         2.6          4.0         1.2 versicolor
#> 10:          5.8         4.0          1.2         0.2     setosa
#> 11:          5.4         3.4          1.7         0.2     setosa
#> 12:          4.5         2.3          1.3         0.3     setosa
#> 13:          5.5         2.6          4.4         1.2 versicolor
#> 14:          6.0         2.9          4.5         1.5 versicolor
#> 15:          6.9         3.1          5.1         2.3  virginica


# use by to slice by group

## following codes get the same results
slice_dt(a,1:3,by = "Species")
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.1         3.5          1.4         0.2
#> 2:     setosa          4.9         3.0          1.4         0.2
#> 3:     setosa          4.7         3.2          1.3         0.2
#> 4: versicolor          7.0         3.2          4.7         1.4
#> 5: versicolor          6.4         3.2          4.5         1.5
#> 6: versicolor          6.9         3.1          4.9         1.5
#> 7:  virginica          6.3         3.3          6.0         2.5
#> 8:  virginica          5.8         2.7          5.1         1.9
#> 9:  virginica          7.1         3.0          5.9         2.1
slice_dt(a,1:3,by = Species)
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.1         3.5          1.4         0.2
#> 2:     setosa          4.9         3.0          1.4         0.2
#> 3:     setosa          4.7         3.2          1.3         0.2
#> 4: versicolor          7.0         3.2          4.7         1.4
#> 5: versicolor          6.4         3.2          4.5         1.5
#> 6: versicolor          6.9         3.1          4.9         1.5
#> 7:  virginica          6.3         3.3          6.0         2.5
#> 8:  virginica          5.8         2.7          5.1         1.9
#> 9:  virginica          7.1         3.0          5.9         2.1
slice_dt(a,1:3,by = .(Species))
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.1         3.5          1.4         0.2
#> 2:     setosa          4.9         3.0          1.4         0.2
#> 3:     setosa          4.7         3.2          1.3         0.2
#> 4: versicolor          7.0         3.2          4.7         1.4
#> 5: versicolor          6.4         3.2          4.5         1.5
#> 6: versicolor          6.9         3.1          4.9         1.5
#> 7:  virginica          6.3         3.3          6.0         2.5
#> 8:  virginica          5.8         2.7          5.1         1.9
#> 9:  virginica          7.1         3.0          5.9         2.1

slice_head_dt(a,2,by = Species)
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.1         3.5          1.4         0.2
#> 2:     setosa          4.9         3.0          1.4         0.2
#> 3: versicolor          7.0         3.2          4.7         1.4
#> 4: versicolor          6.4         3.2          4.5         1.5
#> 5:  virginica          6.3         3.3          6.0         2.5
#> 6:  virginica          5.8         2.7          5.1         1.9
slice_tail_dt(a,2,by = Species)
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.3         3.7          1.5         0.2
#> 2:     setosa          5.0         3.3          1.4         0.2
#> 3: versicolor          5.1         2.5          3.0         1.1
#> 4: versicolor          5.7         2.8          4.1         1.3
#> 5:  virginica          6.2         3.4          5.4         2.3
#> 6:  virginica          5.9         3.0          5.1         1.8

slice_max_dt(a,Sepal.Length,3,by = Species)
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>         <fctr>        <num>       <num>        <num>       <num>
#>  1:     setosa          5.8         4.0          1.2         0.2
#>  2:     setosa          5.7         4.4          1.5         0.4
#>  3:     setosa          5.7         3.8          1.7         0.3
#>  4: versicolor          7.0         3.2          4.7         1.4
#>  5: versicolor          6.9         3.1          4.9         1.5
#>  6: versicolor          6.8         2.8          4.8         1.4
#>  7:  virginica          7.7         3.8          6.7         2.2
#>  8:  virginica          7.7         2.6          6.9         2.3
#>  9:  virginica          7.7         2.8          6.7         2.0
#> 10:  virginica          7.9         3.8          6.4         2.0
#> 11:  virginica          7.7         3.0          6.1         2.3
slice_max_dt(a,Sepal.Length,3,by = Species,with_ties = FALSE)
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.8         4.0          1.2         0.2
#> 2:     setosa          5.7         4.4          1.5         0.4
#> 3:     setosa          5.7         3.8          1.7         0.3
#> 4: versicolor          7.0         3.2          4.7         1.4
#> 5: versicolor          6.9         3.1          4.9         1.5
#> 6: versicolor          6.8         2.8          4.8         1.4
#> 7:  virginica          7.7         3.8          6.7         2.2
#> 8:  virginica          7.7         2.6          6.9         2.3
#> 9:  virginica          7.9         3.8          6.4         2.0
slice_min_dt(a,Sepal.Length,3,by = Species)
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>         <fctr>        <num>       <num>        <num>       <num>
#>  1:     setosa          4.4         2.9          1.4         0.2
#>  2:     setosa          4.3         3.0          1.1         0.1
#>  3:     setosa          4.4         3.0          1.3         0.2
#>  4:     setosa          4.4         3.2          1.3         0.2
#>  5: versicolor          4.9         2.4          3.3         1.0
#>  6: versicolor          5.0         2.0          3.5         1.0
#>  7: versicolor          5.0         2.3          3.3         1.0
#>  8:  virginica          4.9         2.5          4.5         1.7
#>  9:  virginica          5.7         2.5          5.0         2.0
#> 10:  virginica          5.6         2.8          4.9         2.0
slice_min_dt(a,Sepal.Length,3,by = Species,with_ties = FALSE)
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          4.4         2.9          1.4         0.2
#> 2:     setosa          4.3         3.0          1.1         0.1
#> 3:     setosa          4.4         3.0          1.3         0.2
#> 4: versicolor          4.9         2.4          3.3         1.0
#> 5: versicolor          5.0         2.0          3.5         1.0
#> 6: versicolor          5.0         2.3          3.3         1.0
#> 7:  virginica          4.9         2.5          4.5         1.7
#> 8:  virginica          5.7         2.5          5.0         2.0
#> 9:  virginica          5.6         2.8          4.9         2.0

# in `slice_sample_dt`, "by" could only take character class
slice_sample_dt(a,.1,by = "Species")
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>         <fctr>        <num>       <num>        <num>       <num>
#>  1:     setosa          4.6         3.6          1.0         0.2
#>  2:     setosa          4.4         3.0          1.3         0.2
#>  3:     setosa          4.9         3.6          1.4         0.1
#>  4:     setosa          5.1         3.5          1.4         0.2
#>  5:     setosa          4.6         3.2          1.4         0.2
#>  6:     setosa          4.8         3.4          1.6         0.2
#>  7:     setosa          5.5         3.5          1.3         0.2
#>  8:     setosa          4.6         3.1          1.5         0.2
#>  9:     setosa          4.8         3.0          1.4         0.1
#> 10:     setosa          4.4         3.2          1.3         0.2
#> 11:     setosa          5.1         3.8          1.6         0.2
#> 12:     setosa          5.1         3.8          1.5         0.3
#> 13:     setosa          5.5         4.2          1.4         0.2
#> 14:     setosa          4.8         3.1          1.6         0.2
#> 15:     setosa          5.0         3.5          1.6         0.6
#> 16: versicolor          6.0         2.7          5.1         1.6
#> 17: versicolor          5.6         2.7          4.2         1.3
#> 18: versicolor          5.5         2.3          4.0         1.3
#> 19: versicolor          6.6         2.9          4.6         1.3
#> 20: versicolor          5.9         3.0          4.2         1.5
#> 21: versicolor          6.0         3.4          4.5         1.6
#> 22: versicolor          5.7         3.0          4.2         1.2
#> 23: versicolor          5.5         2.6          4.4         1.2
#> 24: versicolor          5.1         2.5          3.0         1.1
#> 25: versicolor          5.5         2.4          3.8         1.1
#> 26: versicolor          5.7         2.9          4.2         1.3
#> 27: versicolor          5.0         2.0          3.5         1.0
#> 28: versicolor          6.1         2.8          4.0         1.3
#> 29: versicolor          5.2         2.7          3.9         1.4
#> 30: versicolor          6.1         3.0          4.6         1.4
#> 31:  virginica          6.0         3.0          4.8         1.8
#> 32:  virginica          6.4         2.8          5.6         2.1
#> 33:  virginica          6.1         2.6          5.6         1.4
#> 34:  virginica          5.9         3.0          5.1         1.8
#> 35:  virginica          7.6         3.0          6.6         2.1
#> 36:  virginica          6.1         3.0          4.9         1.8
#> 37:  virginica          5.8         2.7          5.1         1.9
#> 38:  virginica          6.3         2.5          5.0         1.9
#> 39:  virginica          6.4         3.1          5.5         1.8
#> 40:  virginica          6.7         3.3          5.7         2.1
#> 41:  virginica          7.7         3.0          6.1         2.3
#> 42:  virginica          6.8         3.2          5.9         2.3
#> 43:  virginica          7.9         3.8          6.4         2.0
#> 44:  virginica          7.2         3.0          5.8         1.6
#> 45:  virginica          6.9         3.1          5.4         2.1
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width
slice_sample_dt(a,3,by = "Species")
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.1         3.8          1.5         0.3
#> 2:     setosa          5.0         3.5          1.3         0.3
#> 3:     setosa          5.4         3.9          1.7         0.4
#> 4: versicolor          5.6         2.7          4.2         1.3
#> 5: versicolor          6.2         2.2          4.5         1.5
#> 6: versicolor          4.9         2.4          3.3         1.0
#> 7:  virginica          6.7         3.0          5.2         2.3
#> 8:  virginica          7.7         3.0          6.1         2.3
#> 9:  virginica          6.3         2.5          5.0         1.9
slice_sample_dt(a,51,replace = TRUE,by = "Species")
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>         <fctr>        <num>       <num>        <num>       <num>
#>   1:    setosa          4.7         3.2          1.3         0.2
#>   2:    setosa          4.8         3.0          1.4         0.1
#>   3:    setosa          4.3         3.0          1.1         0.1
#>   4:    setosa          4.4         2.9          1.4         0.2
#>   5:    setosa          4.8         3.4          1.6         0.2
#>  ---                                                            
#> 149: virginica          7.2         3.2          6.0         1.8
#> 150: virginica          6.3         3.3          6.0         2.5
#> 151: virginica          6.3         2.8          5.1         1.5
#> 152: virginica          7.2         3.2          6.0         1.8
#> 153: virginica          5.8         2.7          5.1         1.9