Special Infix Operators
grapes.RdA 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.ycan be unquoted.`%@@%`(x, y) <- value: Assign"@ributes"via infix operator. A friendly version ofattr(x, y) <- value.ycan 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 ofxandy.x %set% y: Subset values inxbyy. Alias forx[x %in% y]. Similar tointersect(x, y)except names and class ofxare maintained.x %!set% y: Subset values inxnot iny. Alias forx[!x %in% y]. Similar tosetdiff(x, y)except names and class ofxare maintained.x %[[% y: Extracts thei^thelement for each ofnelements of a list or data frame, returning either a vector of lengthnor a single row data frame withncolumns. 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