`slice()` 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()` and `slice_tail()` select the first or last rows. * `slice_sample()` randomly selects rows. * `slice_min()` and `slice_max()` select rows with highest or lowest values of a variable.
slice(.data, ...)
slice_head(.data, n)
slice_tail(.data, n)
slice_max(.data, order_by, n, with_ties = TRUE)
slice_min(.data, order_by, n, with_ties = TRUE)
slice_sample(.data, n, replace = FALSE)
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.
When larger than or equal to 1, the number of rows. When between 0 and 1, the proportion of rows to select.
Variable or function of variables to order by.
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.
Should sampling be performed with (`TRUE`) or without (`FALSE`, the default) replacement.
A data.table
a = as.data.table(iris)
slice(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(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_head(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(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(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(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(a,Sepal.Length,10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 7.6 3.0 6.6 2.1 virginica
#> 2: 7.3 2.9 6.3 1.8 virginica
#> 3: 7.2 3.6 6.1 2.5 virginica
#> 4: 7.7 3.8 6.7 2.2 virginica
#> 5: 7.7 2.6 6.9 2.3 virginica
#> 6: 7.7 2.8 6.7 2.0 virginica
#> 7: 7.2 3.2 6.0 1.8 virginica
#> 8: 7.2 3.0 5.8 1.6 virginica
#> 9: 7.4 2.8 6.1 1.9 virginica
#> 10: 7.9 3.8 6.4 2.0 virginica
#> 11: 7.7 3.0 6.1 2.3 virginica
slice_max(a,Sepal.Length,10,with_ties = FALSE)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 7.6 3.0 6.6 2.1 virginica
#> 2: 7.3 2.9 6.3 1.8 virginica
#> 3: 7.2 3.6 6.1 2.5 virginica
#> 4: 7.7 3.8 6.7 2.2 virginica
#> 5: 7.7 2.6 6.9 2.3 virginica
#> 6: 7.7 2.8 6.7 2.0 virginica
#> 7: 7.2 3.2 6.0 1.8 virginica
#> 8: 7.4 2.8 6.1 1.9 virginica
#> 9: 7.9 3.8 6.4 2.0 virginica
#> 10: 7.7 3.0 6.1 2.3 virginica
slice_min(a,Sepal.Length,10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 4.7 3.2 1.3 0.2 setosa
#> 2: 4.6 3.1 1.5 0.2 setosa
#> 3: 4.6 3.4 1.4 0.3 setosa
#> 4: 4.4 2.9 1.4 0.2 setosa
#> 5: 4.3 3.0 1.1 0.1 setosa
#> 6: 4.6 3.6 1.0 0.2 setosa
#> 7: 4.7 3.2 1.6 0.2 setosa
#> 8: 4.4 3.0 1.3 0.2 setosa
#> 9: 4.5 2.3 1.3 0.3 setosa
#> 10: 4.4 3.2 1.3 0.2 setosa
#> 11: 4.6 3.2 1.4 0.2 setosa
slice_min(a,Sepal.Length,10,with_ties = FALSE)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 4.7 3.2 1.3 0.2 setosa
#> 2: 4.6 3.1 1.5 0.2 setosa
#> 3: 4.6 3.4 1.4 0.3 setosa
#> 4: 4.4 2.9 1.4 0.2 setosa
#> 5: 4.3 3.0 1.1 0.1 setosa
#> 6: 4.6 3.6 1.0 0.2 setosa
#> 7: 4.4 3.0 1.3 0.2 setosa
#> 8: 4.5 2.3 1.3 0.3 setosa
#> 9: 4.4 3.2 1.3 0.2 setosa
#> 10: 4.6 3.2 1.4 0.2 setosa
slice_sample(a,10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 4.6 3.2 1.4 0.2 setosa
#> 2: 6.4 2.9 4.3 1.3 versicolor
#> 3: 4.9 3.1 1.5 0.2 setosa
#> 4: 7.2 3.0 5.8 1.6 virginica
#> 5: 6.7 3.1 4.7 1.5 versicolor
#> 6: 7.6 3.0 6.6 2.1 virginica
#> 7: 7.4 2.8 6.1 1.9 virginica
#> 8: 4.9 3.6 1.4 0.1 setosa
#> 9: 5.6 2.9 3.6 1.3 versicolor
#> 10: 6.3 2.8 5.1 1.5 virginica
slice_sample(a,0.1)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 5.9 3.0 5.1 1.8 virginica
#> 2: 6.3 2.5 5.0 1.9 virginica
#> 3: 4.9 3.0 1.4 0.2 setosa
#> 4: 6.1 2.9 4.7 1.4 versicolor
#> 5: 5.1 3.4 1.5 0.2 setosa
#> 6: 4.8 3.4 1.6 0.2 setosa
#> 7: 5.0 3.5 1.3 0.3 setosa
#> 8: 4.5 2.3 1.3 0.3 setosa
#> 9: 6.7 3.1 4.4 1.4 versicolor
#> 10: 5.8 2.7 5.1 1.9 virginica
#> 11: 7.1 3.0 5.9 2.1 virginica
#> 12: 5.0 3.0 1.6 0.2 setosa
#> 13: 5.2 2.7 3.9 1.4 versicolor
#> 14: 4.3 3.0 1.1 0.1 setosa
#> 15: 5.0 3.6 1.4 0.2 setosa