is: Test logical statements on data points

Description Usage Arguments Details Functions Use cases See Also Examples

Description

is* are functions which produce functions of class IsFunction to test logical statements on each data point of an atomic object (vector, matrix, or array). See Details for the general idea, Functions for the short function-specific descriptions and the section Use cases for short examples.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
isPattern(
  pattern.,
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isEqual(
  ref.,
  tol. = .Machine$double.eps^0.5,
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isSame(
  ref.,
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isBetween(
  lwr. = -Inf,
  upr. = Inf,
  open. = c("none", "both", "lwr", "upr"),
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isNegative(
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isPositive(
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isExtremum(
  what. = c("both", "min", "max"),
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isLocalMaximum(
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isLocalMinimum(
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isMaximum(
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

isMinimum(
  strict. = TRUE,
  negate. = FALSE,
  subset. = list(),
  expand. = list(),
  options. = list()
)

Arguments

pattern.

a character regexp pattern, see the 'pattern' argument in grepl

strict.

logical value whether the tested condition is obligatory (TRUE, the default) or optional (FALSE). Only used for combining multiple tests (not available at the moment).

negate.

logical value whether the return value of the function should be negated (default: FALSE). You can also use the standard ! operator for negation which has a specific method for the IsFunction class (see Examples).

subset., expand.

a named list of character, numeric, or logical vectors or a subsetting function (of class IsFunction) indicating which levels of which dimensions to subset or expand on (see Details). If an unnamed list, it must have an attribute 'dim_index', referring to the numeric indices of the dimensions to subset or expand on. Use it only if you are absolutely sure about the datasets you want to test later on!

options.

a list of further arguments passed to the wrapped function (see Functions for details)

ref.

the reference value or a vector (or matrix/array) of reference values.

tol.

tolerance value, the maximum difference which is still tolerated. Can be a vector (or matrix/array) as well.

lwr., upr.

numeric values referring to the lower and upper limit (the defaults are -Inf and Inf, respectively). Both lwr. and upr. can be scalars and vectors as well.

open.

the side of the interval which is open (that is, it does not contain the given endpoint); can be "none" (the default), "both", lwr" or "upr"

what.

specifies whether minima ("min"), maxima ("max") or extrema ("both", the default) should be tested

Details

All is* functions return a function of the class IsFunction which has only one argument, 'x' (the data object to test). 'x' can be an atomic vector, matrix, or array; all other object types result in error.
A second major rule is that all functions returned by an is* function return a logical object of the same shape as the input object. The third rule is that the function returned by any is* function is a hard (strict) or soft (optional) constraint, controled by the 'strict.' argument. This affects the way how the results are combined for joint logical tests (see combine).
Additionally, all is* functions has 'negate.', 'subset.', and 'expand.' arguments. By setting 'negate.' to TRUE, the function can be conceived of as a not* function. The same can be achieved by using the standard negation operator (see Examples).
The 'subset.' and 'expand.' arguments are only considered if the returned function will be called on matrices or arrays. If 'subset.' is provided, the logical condition is tested only on the subsetted slice of the array, and all other data points in the array become FALSE (or TRUE, if negate. is TRUE). The 'expand.' argument does the same except that it expands the subsetted results to the original array instead of fixing the outer-subset data points to FALSE (or TRUE). See Examples.

Functions

Use cases

is* as simple pre-defined test

A very basic use case is to pre-define a logical rule and apply it to several objects. For example one can define
rangeTest <- isBetween(200, 300)
and use this rule to check for whatever numeric or numeric-like character vector, matrix or array whether the values are between 200 and 300. If x denotes the object, rangeTest(x) returns a logical object of the same length and shape as x with TRUEs for values between 200 and 300 and FALSE otherwise.

is* as function argument

is* functions come in especially handy if they are used as function arguments.
For example the code subsetArray(erps, time = isBetween(200, 300)) is a very compact and readable way of subsetting the erps data array on its time dimension, selecting only that part of the array where time >= 200 & time <= 300. Note that this allows the definition of a subsetting rule without knowing in advance how the object which is to be subsetted looks like. For example you can define sub_def <- isBetween(200, 300) beforehand and use sub_def as an argument in subsetArray for all data arrays if all of them have a time dimension measured in the same unit (note that the resolution and the time range may be different).

See Also

combine how to combine is* tests, and isStrict how to check afterwards whether a test is strict or optional

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
# example for isPattern #
#
# note how we pass the 'ignore.case' argument to the internally used
# grepl() function (see ?grepl)
check_start_a <- isPattern("^a", options. = list(ignore.case = TRUE))
check_start_a(c("a", "A", "ba"))

#
# example for isEqual #
#
check_zero <- isEqual(0, tol. = 1e-4)
check_zero(c("0", "0.000001", "1"))


# note that just like for other is* functions, the reference and the
# tolerance can be vectors (or even matrices or arrays), not only scalars,
# and its values are recycled or cropped if necessarily to match the length
# of the 'x' object
check_values <- isEqual(c(0, 1, 2))
check_values(c(0, 1.1, 2, 1e-9))



# it is also possible to base the check only on a subset of x and also
# on a reference slice of x which is then expanded back to the original
# array;
# here we want to test that in the "pre-test" phase of an experiment which
# observations had a width of 1 with 0.1 tolerance (expanded also to height)
check_width <- isEqual(1, tol. = 0.1,
                       subset. = list(time = "pre"),
                       expand. = list(measure = "width")
                       )
( x <- array(c(1.001, 1.6, 1.2, 1.003, 1.01, 1, 4, 3.5),
             dim = c(2, 2, 2),
             dimnames = list(observation = c("a", "b"),
                             measure = c("width", "height"),
                             time = c("pre", "post"))) )
( res <- check_width(x) )

# note that all "time = post" data points are FALSE, and also that
# all height measures are TRUE where width is TRUE
array(paste(x, res, sep = ": "), dim(x), dimnames(x))


# isEqual, just like all other is* functions, can be negated:
check_not_zero <- isEqual(0, negate. = TRUE)
check_not_zero(1)


# the same can be achieved by using ! for negation
check_not_zero2 <- !isEqual(0)
check_not_zero2(1)

#
# example for isSame #
#
check_not_A <- isSame("A", negate. = TRUE)
check_not_A(c("a", "A"))

#
# example for isBetween() #
#
# note that lwr. and upr. might be given in reversed order
check_0_100 <- isBetween(100, 0)
check_0_100(c(-1, 1, 101))

#
# example for isExtremum #
#
# note how we can pass the 'global' and 'tail' arguments to the internal
# findExtrema() function (see ?findExtrema)
check_global_extr <- isExtremum(options. = list(global = TRUE, 
                                                tail = "do_not_care"))
check_global_extr(c(-1, 1, 0, 100, 50))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.