Special Infix Operators
grapes.Rd
A series of useful infix operators, aka "grapes", that can be used to facilitate core functionality, test equality, perform set operations, etc.
Functions
x %@@% y
: A friendly version ofattr(x, y)
to extract"@ribute"
elements.y
can be unquoted.`%@@%`(x, y) <- value
: Assign"@ributes"
via infix operator. A friendly version ofattr(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 ofx
andy
.x %set% y
: Subset values inx
byy
. Alias forx[x %in% y]
. Similar tointersect(x, y)
except names and class ofx
are maintained.x %!set% y
: Subset values inx
not iny
. Alias forx[!x %in% y]
. Similar tosetdiff(x, y)
except names and class ofx
are maintained.x %[[% y
: Extracts thei^th
element for each ofn
elements of a list or data frame, returning either a vector of lengthn
or a single row data frame withn
columns. More efficient alias forpurrr::map_*(x, y)
.
Examples
factor(1:3) %@@% levels
#> [1] "1" "2" "3"
factor(1:3, levels = LETTERS[1:3L]) %@@% levels
#> [1] "A" "B" "C"
mtcars %==% mtcars # equal
#> [1] TRUE
cars2 <- mtcars
cars2 %@@% a <- "foo" # attr assignment; with unquoted 'a'
mtcars %==% cars2 # attr not checked; TRUE
#> [1] TRUE
mtcars %===% cars2 # attr checked; FALSE
#> [1] FALSE
x <- list(a = "b", c = "d", e = "f")
x %set% c("a", "c", "d") # 'c' match
#> $c
#> [1] "d"
#>
x %!set% c("a", "c", "d") # 'b' match
#> $a
#> [1] "b"
#>
#> $e
#> [1] "f"
#>
unlist(x) %!set% c("a", "c", "d") # 'c' match; vector-vector
#> a e
#> "b" "f"
# extract elements of a list
x <- list(a = 1:3, b = 4:6, c = 7:9)
x %[[% 2L
#> a b c
#> 2 5 8
data.frame(x) %[[% 2L # data frame -> same as x[2L, ]
#> a b c
#> 2 2 5 8