Learn R Programming

expss (version 0.5.5)

where: Subsetting Data Frames

Description

cond will be evaluated in the context of the data frame, so columns can be referred to (by name) as variables in the expression (see the examples). .where is version for working with default dataset. See default_dataset. %where% is infix function with the same functional. See examples. There is a special constant .n which equals to number of cases in data for usage in cond expression.

Usage

where(data, cond)
data %where% cond
.where(cond)

Arguments

data
data.frame to be subsetted
cond
logical or numeric expression indicating elements or rows to keep: missing values (NA) are taken as false.

Value

data.frame which contains just selected rows.

Examples

Run this code
# leave only 'setosa'
where(iris, Species == "setosa")
# leave only first five rows
where(iris, 1:5)

# infix version
# note that '%where%' have higher precendence than '=='
# so we need to put condition inside brackets
iris %where% (Species == "setosa")

iris %where% 1:5

# example of .n usage. Very artificial examples
set.seed(42)
train = iris %where% sample(.n, 100)
str(train)

set.seed(42)
test = iris %where% -sample(.n, 100)
str(test)

Run the code above in your browser using DataLab