where: Select variables with a function - copied from tidyselect

View source: R/where.R

whereR Documentation

Select variables with a function - copied from tidyselect

Description

This [selection helper][language] selects the variables for which a function returns 'TRUE'.

Usage

where(fn)

Arguments

fn

A function that returns 'TRUE' or 'FALSE' (technically, a _predicate_ function). Can also be a purrr-like formula.

Examples

“'r, child = "man/rmd/setup.Rmd" “'

Selection helpers can be used in functions like 'dplyr::select()' or ‘tidyr::pivot_longer()'. Let’s first attach the tidyverse:

“'r, comment = "#>", collapse = TRUE library(tidyverse)

# For better printing iris <- as_tibble(iris) “'

'where()' takes a function and returns all variables for which the function returns 'TRUE':

“'r, comment = "#>", collapse = TRUE is.factor(iris[[4]])

is.factor(iris[[5]])

iris

is.numeric(iris[[4]])

is.numeric(iris[[5]])

iris “'

## The formula shorthand

You can use purrr-like formulas as a shortcut for creating a function on the spot. These expressions are equivalent:

“'r, comment = "#>", collapse = TRUE iris

iris

iris “'

The shorthand is useful for adding logic inline. Here we select all numeric variables whose mean is greater than 3.5:

“'r, comment = "#>", collapse = TRUE iris “'


cgpeltier/janesR documentation built on Aug. 9, 2022, 3:22 a.m.