fslice function

Fast Slicing of Matrix-Like Objects

Fast Slicing of Matrix-Like Objects

A fast function to extract rows from a matrix or data frame-like object (by groups).

fslice(x, ..., n = 1, how = "first", order.by = NULL, na.rm = .op[["na.rm"]], sort = FALSE, with.ties = FALSE) fslicev(x, cols = NULL, n = 1, how = "first", order.by = NULL, na.rm = .op[["na.rm"]], sort = FALSE, with.ties = FALSE, ...)

Arguments

  • x: a matrix, data frame or list-like object, including 'grouped_df'.

  • ...: for fslice: names or sequences of columns to group by - passed to fselect. If x is a matrix: atomic vectors to group x. Can be empty to operate on (un)grouped data. For fslicev: further arguments passed to GRP (such as decreasing, na.last, method).

  • cols: select columns to group by, using column names, indices, a logical vector or a selector function (e.g. is_categorical). It can also be a list of vectors, or, if x is a matrix, a single vector.

  • n: integer or proportion (if < 1). Number of rows to select from each group. If a proportion is provided, it is converted to the equivalent number of rows.

  • how: character. Method to select rows. One of:

    • "first": select first n rows
    • "last": select last n rows
    • "min": select n rows with minimum values of order.by
    • "max": select n rows with maximum values of order.by
  • order.by: vector or column name to order by when how is "min" or "max". Must be same length as rows in x. In fslice it must not be quoted.

  • na.rm: logical. If TRUE, missing values in order.by are removed before selecting rows.

  • sort: logical. If TRUE, sort selected rows on the grouping columns. FALSE uses first-appearance order (including grouping columns if how is "first" or "last") - fastest.

  • with.ties: logical. If TRUE and how is "min" or "max", returns all rows with the extreme value. Currently only supported for n = 1 and sort = FALSE.

Returns

A subset of x containing the selected rows.

See Also

fsubset, fcount, Data Frame Manipulation , Collapse Overview

Examples

# Basic usage fslice(mtcars, n = 3) # First 3 rows fslice(mtcars, n = 3, how = "last") # Last 3 rows fslice(mtcars, n = 0.1) # First 10% of rows # Using order.by fslice(mtcars, n = 3, how = "min", order.by = mpg) # 3 cars with lowest mpg fslice(mtcars, n = 3, how = "max", order.by = mpg) # 3 cars with highest mpg # With grouping mtcars |> fslice(cyl, n = 2) # First 2 cars per cylinder mtcars |> fslice(cyl, n = 2, sort = TRUE) # with sorting (slightly less efficient) mtcars |> fslice(cyl, n = 2, how = "min", order.by = mpg) # 2 lowest mpg cars per cylinder # Using with.ties mtcars |> fslice(cyl, n = 1, how = "min", order.by = mpg, with.ties = TRUE) # With grouped data mtcars |> fgroup_by(cyl) |> fslice(n = 2, how = "max", order.by = mpg) # 2 highest mpg cars per cylinder
  • Maintainer: Sebastian Krantz
  • License: GPL (>= 2) | file LICENSE
  • Last published: 2025-03-10