grapes: The Grapevine

grapesR Documentation

The Grapevine

Description

Commonly referred to as "binary" or "infix" operators, functions that have a left and right side are typically defined using %% symbols. Thus the colloquial term "grapes". The functions below represent a growing list of useful binary operators that come in handy when working in an R environment.

Usage

x %!in% y

x %||% y

x %null% y

x %@@% y

x %@@% y <- value

x %==% y

x %!=% y

x %===% y

x %set% y

x %!set% y

x %[[% y

Arguments

x

The left hand side of the infix operator.

y

The right hand side of the infix operator.

value

New value for attribute y.

Functions

  • x %!in% y: A friendly version of !x %in% y.

  • x %||% y: A friendly version of if (length(x) == 0) y else x for NULL objects.

  • x %null% y: If x = NULL or x = "", return NULL, otherwise y.

  • x %@@% y: A friendly version of attr(x, y). y can be unquoted.

  • `%@@%`(x, y) <- value: Assign attributes via infix operator. A friendly version of attr(x, y) <- value. y can be unquoted.

  • x %==% y: A gentler logical test for equality of two objects. Attributes are not checked. Use ⁠%===%⁠ to check attributes.

  • x %!=% y: A logical test that two objects are not equal.

  • x %===% y: Also tests attributes of x and y.

  • x %set% y: Subset values in x by y. Alias for x[x %in% y]. Similar to intersect(x, y) except names and class of x are maintained.

  • x %!set% y: Subset values in x not in y. Alias for x[!x %in% y]. Similar to setdiff(x, y) except names and class of x are maintained.

  • x %[[% y: Extracts the ith element for each of n elements of a list or data frame, returning either a vector of length n or a single row data frame with n columns.

See Also

intersect(), setdiff(), all.equal(), isTRUE()

Examples

foo <- NULL
foo %||% "bar"     # 'bar'
"foo" %||% "bar"   # 'foo'

factor(1:3) %@@% levels
factor(1:3, levels = LETTERS[1:3]) %@@% levels
mtcars %@@% row.names

mtcars %==% mtcars       # equal
cars2 <- mtcars
cars2 %@@% a <- "foo"  # attr assignment; with unquoted 'a'
mtcars %==% cars2        # attr not checked; TRUE
mtcars %===% cars2       # attr checked; FALSE

x <- list(a = "b", c = "d", e = "f")
x %set% c("a", "c", "d")   # 'c' match
x %!set% c("a", "c", "d")  # 'b' match
unlist(x) %!set% c("a", "c", "d")   # 'c' match; vector-vector

x <- list(a = 1:2, b = 3:4, c = 5:6)
x %[[% 2L

data.frame(x) %[[% 2L

stufield/grapevine documentation built on April 5, 2025, 4:19 a.m.