fsubset returns subsets of vectors, matrices or data frames which meet conditions. It is programmed very efficiently and uses C source code from the data.table package. Especially for data frames it is significantly (4-5 times) faster than subset or dplyr::filter. The methods also provide enhanced functionality compared to subset. The function ss provides an (internal generic) programmers alternative to [ that does not drop dimensions and is significantly faster than [.data.frame.

fsubset(x, ...)
sbt(x, ...)     # Shortcut for fsubset

# S3 method for default
fsubset(x, subset, ...)

# S3 method for matrix
fsubset(x, subset, ..., drop = FALSE)

# S3 method for data.frame
fsubset(x, subset, ...)

# Fast subsetting (replaces [, programmers choice)
ss(x, i, j)

## Arguments

x object to be subsetted. logical expression indicating elements or rows to keep: missing values are taken as FALSE. The default and matrix methods only support logical vectors or row-indices (or a character vector of rownames if the matrix has rownames; the data frame method also supports logical vectors or row-indices). For the matrix or data frame method: multiple comma-separated expressions indicating columns to select. Otherwise: further arguments to be passed to or from other methods. passed on to [ indexing operator. Only available for the matrix method. positive or negative row-indices or a logical vector to subset the rows of x. a vector of column names, positive or negative indices or a suitable logical vector to subset the columns of x. Note: Negative indices are converted to positive ones using j <- seq_along(x)[j].

## Details

fsubset is a generic function, with methods supplied for vectors, matrices, and data frames (including lists). It represents an improvement in both speed and functionality over subset. The function ss is an improvement of [ to subset (vectors) matrices and data frames without dropping dimensions. It is significantly faster than [.data.frame. For subsetting columns alone, please see selecting and replacing columns.

For ordinary vectors, the result is .Call(C_subsetVector, x, subset), where C_subsetVector is an internal function in the data.table package. The subset can be integer or logical. Appropriate errors are delivered for wrong use.

For matrices the implementation is all base-R but slightly more efficient and more versatile than subset.matrix. Thus it is possible to subset matrix rows using logical or integer vectors, or character vectors matching rownames. The drop argument is passed on to the indexing method for matrices.

For both matrices and data frames, the ... argument can be used to subset columns, and is evaluated in a non-standard way. Thus it can support vectors of column names, indices or logical vectors, but also multiple comma separated column names passed without quotes, each of which may also be replaced by a sequence of columns i.e. col1:coln (see examples).

For data frames, the subset argument is also evaluated in a non-standard way. Thus next to vector of row-indices or logical vectors, it supports logical expressions of the form col2 > 5 & col2 < col3 etc. (see examples). The data frame method uses C_subsetDT, an internal C function from the data.table package to subset data frames, hence it is significantly faster than subset.data.frame. If fast data frame subsetting is required but no non-standard evaluation, the function ss is slightly simpler and faster.

Factors may have empty levels after subsetting; unused levels are not automatically removed. See fdroplevels for a way to drop all unused levels from a data frame.

## Value

An object similar to x containing just the selected elements (for a vector), rows and columns (for a matrix or data frame).

fselect, get_vars, ftransform, Data Frame Manipulation, Collapse Overview

## Examples

fsubset(airquality, Temp > 90, Ozone, Temp)
#>    Ozone Temp
#> 1     NA   93
#> 2     NA   92
#> 3     97   92
#> 4     97   92
#> 5     NA   91
#> 6     NA   92
#> 7     76   97
#> 8    118   94
#> 9     84   96
#> 10    85   94
#> 11    96   91
#> 12    78   92
#> 13    73   93
#> 14    91   93fsubset(airquality, Day == 1, -Temp)
#>   Ozone Solar.R Wind Month Day
#> 1    41     190  7.4     5   1
#> 2    NA     286  8.6     6   1
#> 3   135     269  4.1     7   1
#> 4    39      83  6.9     8   1
#> 5    96     167  6.9     9   1fsubset(airquality, Day == 1, -(Day:Temp))
#>   Ozone Solar.R Wind
#> 1    41     190  7.4
#> 2    NA     286  8.6
#> 3   135     269  4.1
#> 4    39      83  6.9
#> 5    96     167  6.9fsubset(airquality, Day == 1, Ozone:Wind)
#>   Ozone Solar.R Wind
#> 1    41     190  7.4
#> 2    NA     286  8.6
#> 3   135     269  4.1
#> 4    39      83  6.9
#> 5    96     167  6.9fsubset(airquality, Day == 1 & !is.na(Ozone), Ozone:Wind, Month)
#>   Ozone Solar.R Wind Month
#> 1    41     190  7.4     5
#> 2   135     269  4.1     7
#> 3    39      83  6.9     8
#> 4    96     167  6.9     9
ss(airquality, 1:10, 2:3)         # Significantly faster than airquality[1:10, 2:3]
#>    Solar.R Wind
#> 1      190  7.4
#> 2      118  8.0
#> 3      149 12.6
#> 4      313 11.5
#> 5       NA 14.3
#> 6       NA 14.9
#> 7      299  8.6
#> 8       99 13.8
#> 9       19 20.1
#> 10     194  8.6fsubset(airquality, 1:10, 2:3)    # This is possible but not advised
#>    Solar.R Wind
#> 1      190  7.4
#> 2      118  8.0
#> 3      149 12.6
#> 4      313 11.5
#> 5       NA 14.3
#> 6       NA 14.9
#> 7      299  8.6
#> 8       99 13.8
#> 9       19 20.1
#> 10     194  8.6