Skip to contents

This function filters a dataframe or survey design object by applying a specified boolean condition to one of its numeric variables. The resulting filtered dataframe is returned, along with the tidyverse code used to generate it.

Usage

filter_num(data, var, op = c("<=", "<", ">=", ">", "==", "!="), num)

Arguments

data

A dataframe or survey design object to be filtered.

var

The name of the column in data to be filtered by.

op

A logical operator to apply for the filtering condition. Valid options are: "<=", "<", ">=", ">", "==", or "!=".

num

The numeric value for which the specified op is applied.

Value

A filtered dataframe with the tidyverse code attached.

See also

Author

Owen Jin, Tom Elliott, Zhaoming Su

Examples

filtered <- filter_num(iris, var = "Sepal.Length", op = "<=", num = 5)
cat(code(filtered))
#> iris |> dplyr::filter(Sepal.Length <= 5)
head(filtered)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          4.9         3.0          1.4         0.2  setosa
#> 2          4.7         3.2          1.3         0.2  setosa
#> 3          4.6         3.1          1.5         0.2  setosa
#> 4          5.0         3.6          1.4         0.2  setosa
#> 5          4.6         3.4          1.4         0.3  setosa
#> 6          5.0         3.4          1.5         0.2  setosa

library(survey)
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#> 
#> Attaching package: ‘survey’
#> The following object is masked from ‘package:graphics’:
#> 
#>     dotchart
data(api)
svy <- svydesign(~ dnum + snum,
    weights = ~pw, fpc = ~ fpc1 + fpc2,
    data = apiclus2
)
svy_filtered <- filter_num(svy, var = "api00", op = "<", num = 700)
cat(code(svy_filtered))
#> svy |> srvyr::as_survey() |> srvyr::filter(api00 < 700)