sfilter {labelr}R Documentation

Safely Filter Rows of a Labeled Data Frame

Description

sfilter allows one to filter-subset a data.frame, selecting only rows that satisfy conditions (after the fashion of base::subset() or dplyr::filter()), returning the selected rows as a data.frame that preserves the labelr attributes attached to the inputted data.frame.

Usage

sfilter(data, condition)

Arguments

data

the data.frame from which columns will be selected.

condition

row-filtering conditions along the lines of base::subset() and/or dplyr::select().

Details

This function accepts a data.frame, followed by conditional filtering instructions and returns the selected rows (and all inputted data.frame columns) as a data.frame that preserves the labelr attribute information of the originally supplied data.frame. See ssubset for a variant that combines sfilter row-filtering and sselect column selection in a single function. See sbrac for a labelr attribute-preserving approach to row and/or column indexing. See also sdrop, ssort, srename, slab, and flab.

Value

a labelr label attribute-preserving data.frame consisting of the selected rows that meet the filtering condition(s).

Examples

# make toy demographic (gender, raceth, etc.) data set
set.seed(555)
df <- make_demo_data(n = 1000) # another labelr:: function
# let's add variable VALUE labels for variable "raceth"
df <- add_val_labs(df,
  vars = "raceth", vals = c(1:7),
  labs = c("White", "Black", "Hispanic", "Asian", "AIAN", "Multi", "Other"),
  max.unique.vals = 50
)

head(df, 3)
check_labs_att(df, "val.labs.raceth") # "raceth" lab specifically TRUE

dffil1 <- sfilter(df, raceth %in% c(1, 2, 3) & id < 50 & gender == 1)
head(dffil1, 3)
check_labs_att(dffil1, "val.labs.raceth") # "raceth" lab specifically TRUE

dffil2 <- sfilter(df, !raceth %in% 1:5 | (age == 60))
head(dffil2, 8)
check_labs_att(dffil2, "val.labs.raceth") # "raceth" lab specifically TRUE

[Package labelr version 0.1.5 Index]