sset: Cheaper subset 'sset()'

View source: R/sset.R

ssetR Documentation

Cheaper subset sset()

Description

sset() is a cheaper alternative to [.

It consistently subsets data frame rows for any data frame class including tibble and data.table.

Usage

sset(x, i = NULL, j = NULL, ...)

Arguments

x

Vector or data frame.

i

A logical vector or integer vector of locations.

j

Column indices, names or logical vector.

...

Further parameters passed to [.

Details

S3 dispatching

sset will internally dispatch the correct method and will call [ if it can't find an appropriate method. This means one can define their own [ method for custom S3 objects.

To speed up subsetting for common objects likes Dates and POSIXlt an internal generic function is used which overwrites the [ method for that common object. This is why subsetting POSIXlt is much faster with sset an internal method has been defined. For more details see the code for cheapr:::cheapr_sset.

Difference to base R

When i is a logical vector, it is passed directly to which_().
This means that NA values are ignored and this also means that i is not recycled, so it is good practice to make sure the logical vector matches the length of x. To return NA values, use sset(x, NA_integer_).

ALTREP range subsetting

When i is an ALTREP compact sequence which can be commonly created using e.g. 1:10 or using seq_len, seq_along and seq.int, sset internally uses a range-based subsetting method which is faster and doesn't allocate i into memory.

Value

A new vector, data frame, list, matrix or other R object.

Examples

library(cheapr)
library(bench)

# Selecting columns
sset(airquality, j = "Temp")
sset(airquality, j = 1:2)

# Selecting rows
sset(iris, 1:5)

# Rows and columns
sset(iris, 1:5, 1:5)
sset(iris, iris$Sepal.Length > 7, c("Species", "Sepal.Length"))

# Comparison against base
x <- rnorm(10^4)

mark(x[1:10^3], sset(x, 1:10^3))
mark(x[x > 0], sset(x, x > 0))

df <- data.frame(x = x)

mark(df[df$x > 0, , drop = FALSE],
     sset(df, df$x > 0),
     check = FALSE) # Row names are different


cheapr documentation built on Nov. 28, 2025, 5:06 p.m.