Learn R Programming

mvbutils (version 1.0.1)

mvbutils-operators: Utility operators

Description

Succinct or convenience operators

Usage

a %&% b
a %!in% b
x %is.not.a% what
x %is.a% what
x %is.not.an% what
x %is.an% what
vector %except% condition
x %matching% patt
x %without.name% what
a %in.range% b
from %upto% to
from %downto% to
x %where% cond

Value

  • %&%string concatenation via paste( a, b, sep="")
  • %!in%logical negation of %in%
  • %is.a%(and %is.not.a%, %is.an%, %is.not.an%) result of inherits to test class membership
  • %except%vector except those elements that match something in condition
  • %matching%those elements in x which are regexp-matched with any of the patts
  • %without.name%x except those elements whose names match what
  • %in.range%those elements of a that are >=min(b) and <=max(b)< description="">
  • %upto%like : except that if to, numeric(0) is returned (useful for coding for-loops)
  • %downto%converse of %upto%
  • %where%those rows of the data.frame x where cond is satisfied (see DETAILS)

Arguments by function

%&%{a, b:character vectors to be pasted with no separator} %!in%{a, b:vectors (character, numeric, complex, or logical).} %except%{vector, condition:character or numeric vectors} %in.range%{a, b:numeric vectors.} %is.a%, etc.{x:object whose class is to be checked} %is.a%, etc.{what:class name} %matching%{x:character vector} %matching%{patt:character vector of regexps} %upto%, %downto%{from, to:numeric(1)} %where%{x:data.frame} %where%{cond:unquoted expression to be evaled in context of x, then in the calling frame of %where% (or .GlobalEnv). Wrap cond in parentheses to avoid trouble with operator precedence.} %without.name%{x:object with names attribute} %without.name%{what:character vector of names to drop}

Examples

Run this code
"a" %&% "b" # "ab"
 1:5 %!in% 3:4 # c( TRUE, TRUE, FALSE, FALSE, TRUE)
 trf <- try( 1+"nonsense")
 if( trf %is.not.a% "try-error") cat( "OK\n") else cat( "not OK\n")
 1:5 %except% c(2,4,6) # c(1,3,5)
 c( alpha=1, beta=2) %without.name% "alpha" # c( beta=2)
 1:5 %in.range% c( 2, 4) # c(F,T,T,T,F)
 c( "cat", "hat", "dog", "brick") %matching% c( "at", "ic") # cat hat brick
 1 %upto% 2 # 1:2
 1 %upto% 0 # numeric( 0); using \%upto\% rather than : in for-loops can simplify coding
 1 %downto% 0 # 1:0
 1 %downto% 2 # numeric( 0)
 ff <- function( which.row) {
 x <- data.frame( a=1:3, b=4:6)
 x %where% (a==which.row)
 }
 ff( 2) # data.frame( a=2, b=5)

Run the code above in your browser using DataLab