Learn R Programming

expss (version 0.8.6)

criteria: Criteria functions

Description

These functions returns criteria functions which could be used in different situation - see keep, except, recode, 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) are defined for these functions. List of functions:

  • gt greater than

  • ge/gte greater than or equal

  • eq equal

  • ne/neq not equal

  • lt less than

  • le/lte less than or equal

  • thru checks whether value is inside interval. thru(0,1) is equivalent of x>=0 & x<=1 or ge(0) & le(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

  • items return TRUE for elements of vector with given sequential number. It is intended for usage with keep, except

Usage

eq(x)

ne(x)

neq(x)

lt(x)

gt(x)

le(x)

lte(x)

ge(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

from(x)

to(x)

items(...)

not_na(x)

other(x)

as.criterion(crit)

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

...

numeric indexes of desired items

crit

vector of values/function which returns logical or vector. It will be converted to function of class criterion.

Value

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

See Also

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

Examples

Run this code
# NOT RUN {
# 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(ne(75), df1$b) # not equal 75 = 3

count_if(ge(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"))

# except first column
iris %n_d% items(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)
recode(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, 11 %thru% hi ~ 3, other ~ 0)
# the same result
recode(qvar, 1 %thru% 5 ~ 1, 6 %thru% 10 ~ 2, ge(11) ~ 3, other ~ 0)


# }

Run the code above in your browser using DataLab