Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

where

Subset (filter) data.frames/matrices/vectors/lists


Description

For the data frame cond will be evaluated in the data.frame's context. So columns can be referred as variables in the expression (see the examples). If data is list then where will be applied to each element of the list. For other types (vector/matrix) there is no non-standard evaluation. There is a special constant .N which equals to number of rows in data for usage in cond expression. .where is version for working with default dataset. See default_dataset.

Usage

where(data, cond)

.where(cond)

Arguments

data

data.frame/matrix/vector/list to be subsetted

cond

logical or numeric expression indicating elements or rows to keep: missing values (NA) are taken as FALSE. If data is data.frame then cond will be evaluated in the scope of the data.

Value

data.frame/matrix/vector/list which contains just selected rows.

Examples

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


# example of .N usage. 
set.seed(42)
train = where(iris, sample(.N, 100))
str(train)

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

# list example
set.seed(123)
rand_matr = matrix(sample(10, 60, replace = TRUE), ncol = 3)
rand_vec = sample(10, 20, replace = TRUE)
my_list = list(iris, rand_matr, rand_vec)
# two random elements from the each list item
where(my_list, sample(.N, 2))

expss

Tables, Labels and Some Useful Functions from Spreadsheets and 'SPSS' Statistics

v0.10.7
GPL (>= 2)
Authors
Gregory Demin [aut, cre], Sebastian Jeworutzki [ctb] (<https://orcid.org/0000-0002-2671-5253>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.