Learn R Programming

expss (version 0.5.1)

criteria: Criteria functions

Description

These functions returns criteria functions which could be used in different situation - see 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 greater than or equal
  • eq equal
  • neq not equal
  • lt less than
  • lte 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

Usage

eq(x)
neq(x)
lt(x)
gt(x)
lte(x)
gte(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

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

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

# 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)

# 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% Inf ~ 3, . ~ 0)
# the same result
if_val(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, gte(11) ~ 3, . ~ 0)


Run the code above in your browser using DataLab