Learn R Programming

expss (version 0.5.5)

criteria: Criteria functions

Description

These functions returns criteria functions which could be used in different situation - see keep, except, if_val, na_if, %i%, %d%, count_if, match_row etc. For example, gt(5) returns function which tests whether its argument greater than five. fixed("apple") return function which tests whether its argument contains "apple". Logical operations (|, &, !, xor) defined for these functions. List of functions:
  • gt greater than
  • gte/ge greater than or equal
  • eq equal
  • neq/ne not equal
  • lt less than
  • lte/le less than or equal
  • thru checks whether value is inside interval. thru(0,1) is equivalent of x>=0 & x<=1< code=""> or gte(0) & lte(1)
  • %thru% infix version of thru, e. g. 0 %thru% 1
  • regex use POSIX 1003.2 extended regular expressions. For details see grepl
  • perl perl-compatible regular expressions. For details see grepl
  • fixed pattern is a string to be matched as is. For details see grepl
  • to returns function which gives TRUE for all elements of vector before the first occurrence of x and for x.
  • from returns function which gives TRUE for all elements of vector after the first occurrence of x and for x. from and to are intended for usage with keep and except.
  • not_na return TRUE for all non-NA elements of vector.
  • other return TRUE for all elements of vector. It is intended for usage with if_val, keep, except

Usage

eq(x)
neq(x)
ne(x)
lt(x)
gt(x)
lte(x)
le(x)
gte(x)
ge(x)
perl(pattern, ignore.case = FALSE, useBytes = FALSE)
regex(pattern, ignore.case = FALSE, useBytes = FALSE)
fixed(pattern, ignore.case = FALSE, useBytes = FALSE)
thru(lower, upper)
lower %thru% upper
from(x)
to(x)
not_na(x)
other(x)

Arguments

x
vector
pattern
character string containing a regular expression (or character string for fixed) to be matched in the given character vector. Coerced by as.character to a character string if possible.
ignore.case
logical see grepl
useBytes
logical see grepl
lower
vector/single value - lower bound of interval
upper
vector/single value - upper bound of interval

Value

function of class 'criterion' which tests its argument against condition and return logical value

See Also

keep, except, count_if, match_row, if_val, na_if, %i%, %d%

Examples

Run this code
# operations on vector
1:6 %d% gt(4) # 1:4

1:6 %d% (1 | gt(4)) # 2:4

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

letters %i% from("w")  # w, x, y, z

letters %i% to("c")  # a, b, c

letters %i% (from("b") & to("e"))  # b, d, e

c(1, 2, NA, 3) %i% other # c(1, 2, 3)

# examples with count_if
df1 = data.frame(
    a=c("apples",   "oranges",     "peaches",     "apples"),
    b = c(32, 54, 75, 86)
)

count_if(gt(55), df1$b) # greater than 55 = 2

count_if(neq(75), df1$b) # not equal 75 = 3

count_if(gte(32), df1$b) # greater than or equal 32 = 4

count_if(gt(32) & lt(86), df1$b) # greater than 32 and less than 86 = 2

# via different kinds of 'thru'
count_if(thru(35, 80), df1$b) # greater than or equals to 35 and less than or equals to 80 = 2
# infix version
count_if(35 %thru% 80, df1$b) # greater than or equals to 35 and less than or equals to 80 = 2

# values that started on 'a'
count_if(regex("^a"),df1) # 2

# count_row_if
count_row_if(regex("^a"),df1) # c(1,0,0,1)

# examples with 'keep' and 'except'

data(iris)
iris %keep% to("Petal.Width") # column 'Species' will be removed 
 
# 'Sepal.Length', 'Sepal.Width' will be left 
iris %except% from("Petal.Length") 

# if_val examples
# From SPSS: RECODE QVAR(1 THRU 5=1)(6 THRU 10=2)(11 THRU HI=3)(ELSE=0).
set.seed(123)
qvar = sample((-5):20, 50, replace = TRUE)
if_val(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, 11 %thru% hi ~ 3, other ~ 0)
# the same result
if_val(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, gte(11) ~ 3, other ~ 0)


Run the code above in your browser using DataLab