Learn R Programming

expss (version 0.7.1)

vectors: Infix operations on vectors - append, diff, intersection, union, replication

Description

  • %a% a(ppends) second argument to first argument. See also append.

  • %u% and v_union u(nite) first and second arguments. Remove elements from second argument which exist in first argument.

  • %d% and v_diff d(iff) second argument from first argument. Second argument could be a function which returns logical value. In this case elements of first argument which give TRUE will be removed.

  • %i% and v_intersect i(ntersect) first argument and second argument. Second argument could be a function which returns logical value. In this case elements of first argument which give FALSE will be removed.

  • %e% and v_xor e(xclusive OR). Returns elements that contained only in one of arguments.

  • %r% r(epeats) first argument second argument times. See also rep.

  • %n_d% and n_diff n(ames) d(iff) - diff second argument from names of first argument. Second argument could be a function which returns logical value. In this case elements of first argument which names give TRUE will be removed.

  • %n_i% and n_intersect n(ames) i(ntersect) - intersect names of first argument with second argument. Second argument could be a function which returns logical value. In this case elements of first argument which names give FALSE will be removed.

All these functions except %n_d%, %n_i% preserve names of vectors and don't remove duplicates. For %d%, %i%, %n_d%, %n_i% one can use criteria functions. See criteria for details.

Usage

e1 %a% e2

v_union(e1, e2)

e1 %u% e2

v_diff(e1, e2)

e1 %d% e2

v_intersect(e1, e2)

e1 %i% e2

v_xor(e1, e2)

e1 %e% e2

e1 %r% e2

n_intersect(e1, e2)

e1 %n_i% e2

n_diff(e1, e2)

e1 %n_d% e2

Arguments

e1

vector (possibly data.frame/matrix/list for %n_d%, %n_i%)

e2

vector (or function for %d%, %i%)

Value

vector (possibly data.frame/matrix/list for %n_d%, %n_i%)

Examples

Run this code
# NOT RUN {
1:4 %a% 5:6   # 1:6

1:4 %a% 4:5   # 1,2,3,4,4,5

1:4 %u% 4:5   # 1,2,3,4,5

1:6 %d% 5:6   # 1:4

# function as criterion
1:6 %d% gt(4) # 1:4

1:4 %i% 4:5   # 4

# function as criterion
letters %i% perl("[a-d]") # a,b,c,d

# function as criterion 
letters %i% (fixed("a") | fixed("z")) # a, z

1:4 %e% 4:5   # 1, 2, 3, 5

1:2 %r% 2     # 1, 2, 1, 2

# %n_i%, %n_d%

# remove column Species
iris %n_d% "Species" 

# leave only columns which names start with "Sepal"
iris %n_i% perl("^Sepal") 

# leave column "Species" and columns which names start with "Sepal" 
iris %n_i% (perl("^Sepal")|"Species") 

# }

Run the code above in your browser using DataLab