knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

To start, load the package.

library(extraoperators)

Logical Comparisons

This section covers basic logical comparisons and shows how they might be done in base R versus using the extraoperators package. Many of these are quite simple, but are defined so that later operators are possible.

First let's define our "data" as some numbers stored in sample_numbers.

sample_numbers <- c(9, 1, 5, 3, 4, 10, 99)

Now we can do a series of simple logical comparisons which return a logical vector of TRUE or FALSE.

## base R: greater than 3?
sample_numbers > 3

## base R: greater than or equal to 3?
sample_numbers >= 3

## base R: less than 3?
sample_numbers < 3

## base R: less than or equal to 3?
sample_numbers <= 3

Unfortunately, we cannot use < or > in custom operators, so we use the substitutions: g = > and l = < and e = =. So that ge = <= etc.

## extraoperators: greater than 3?
sample_numbers %g% 3

## extraoperators: greater than or equal to 3?
sample_numbers %ge% 3

## extraoperators: less than 3?
sample_numbers %l% 3

## extraoperators: less than or equal to 3?
sample_numbers %le% 3

So far there is no real gain in using extraoperators but this changes for more complex operations. What if we want to know if our values fall within some range? This is a fairly common task, such as saying that valid ages must be between 0 and 100 years.

## base R: greater than 3 and less than 10?
sample_numbers > 3 & sample_numbers < 10

## base R: greater than or equal to 3 and less than 10?
sample_numbers >= 3 & sample_numbers < 10

## base R: greater than 3 and less than or equal to 10?
sample_numbers > 3 & sample_numbers <= 10

## base R: greater than or equal to 3 and less than or equal to 10?
sample_numbers >= 3 & sample_numbers <= 10

Base R accomplishes this through chaining of operations. extraoperators has built in range operators.

## extraoperators: greater than 3 and less than 10?
sample_numbers %gl% c(3, 10)

## extraoperators: greater than or equal to 3 and less than 10?
sample_numbers %gel% c(3, 10)

## extraoperators: greater than 3 and less than or equal to 10?
sample_numbers %gle% c(3, 10)

## extraoperators: greater than or equal to 3 and less than or equal to 10?
sample_numbers %gele% c(3, 10)

Finally, extraoperators includes a not in operator, %!in%.

## base R: not in 3 or 10
!sample_numbers %in% c(3, 10)

## extraoperators: not in 3 or 10
sample_numbers %!in% c(3, 10)

The next sections show a few examples of these operators augmented by prefixes: ?, s and a.

Indices (Which Values?)

Sometimes we want to use a logical comparison and identify indices, such as to use in a loop. extraoperators does this by prefixing operators with ? for "which".

## base R: what are the indices that match 3 and 10?
which(sample_numbers %in% c(3, 10))

## extraoperators: what are the indices that match 3 and 10?
sample_numbers %?in% c(3, 10)

## base R: what are the indices for numbers between 3 and 10?
which(sample_numbers > 3 & sample_numbers < 10)

## extraoperators: what are the indices for numbers between 3 and 10?
sample_numbers %?gl% c(3, 10)

This can be readily incorporated in other code for further processing.

Subsetting

Another fairly common task is selecting only certain observations. For example, we might want to calculate the average of numbers within a plausible range (e.g., excluding outliers). In extraoperators subsetting is done by adding an s prefix.

## base R: subset to only numbers between 3 and 10
mean(subset(sample_numbers, sample_numbers > 3 & sample_numbers < 10))

## or equivalently 
mean(sample_numbers[sample_numbers > 3 & sample_numbers < 10])

## extraoperators: subset to only numbers between 3 and 10
mean(sample_numbers %sgl% c(3, 10))

Subsetting can be especially useful in quick exploratory analyses. Graphs are easily hard to read if there are extreme values. Subsetting makes it fast to "zoom in" on a specific range.

All (or None)

Finally, you might have some quality controls in place for data. For example asserting that all ages are between 0 and 100. In extraoperators this is done by adding the prefix a.

## base R: are all numbers between 0 and 10?
all(sample_numbers > 0 & sample_numbers < 10)

## extraoperators: are all numbers between 0 and 10?
sample_numbers %agl% c(0, 10)

## extraoperators: are all numbers between 0 and 100?
sample_numbers %agl% c(0, 100)

If you want to know the opposite, are no numbers between 0 and 100, we can negate the whole operation.

## extraoperators: are NO numbers between 0 and 100?
!sample_numbers %agl% c(0, 100)

## extraoperators: are NO numbers between 55 and 60?
!sample_numbers %agl% c(55, 60)

There are also expanded all, subset, and which operators for equals and not equals.

## extraoperators: are all values equal?
sample_numbers %a==% sample_numbers

## extraoperators: are all values NOT equal?
c(1, 3, 5) %a!=% c(5, 1, 3)

Chaining

In language, it is fairly natural to make a statement like this: "In my study, age should be between 18 to 65 and not be missing." In R, the usual implementation of this is more equivalent to: "In my study, age should be greater than 18 and age should be less than 65 and age should not be missing." extraoperators tries to facilitate something closer to the cleaner original statement using the chaining operator, %c%. The chaining operator chains a set of operations on the right hand side with the argument on its left hand side passed to each. To accomplish this, the right hand side must be quoted.

age <- c(19, 30, 90, 50, NA, 45)
age %c% "(> 18 & < 65) & !is.na"

Because the right hand side of the chaining operator is a character string that is parsed, it is possible to do some special things in it. is.na, !is.na, is.nan, and !is.nan are special characters that do not require any further value to work correctly. As shown, parentheses also work, which allows fine grained control over exactly what is intended. For example if we expect only adults are in the study, but those who refused to report their age were coded -9 and people who failed to complete the questionnaire at all are missing.

age <- c(19, 30, 90, 50, NA, 16, -9)
age %c% "(> 18 | == -9) & !is.na"

As with all operators, there are prefixes for all, subset, and which.

age %ac% "(> 18 | == -9) & !is.na"

age %ac% "> -Inf"
age %ac% "> -Inf & !is.na"
age %ac% "> -Inf | is.na"

age %sc% "(> 18 | == -9) & !is.na"

age %?c% "(> 18 | == -9) & !is.na"

Interval Notation Operator

In math, interval notation often is used. For example, we might write: (x \in (1, 5) \cup [6, \infty)) to indicate that x is between the intervals 1 to 5 (not including 1 or 5) or between 6 and positive infinity, including 6 but not positive infinity. The interval notation operator, %e% let's you use fairly similar language in R. "|" is the union operator and "&" is the intersect operator. Variables are allowed but no functions as these cannot be parsed.

c(1, 2, 6, 300) %e% "(1, 5) | [6, Inf)"

## this is OK
x <- max(mtcars$mpg)
c(1, 2, 6, 300) %e% "(1, 5) | [6, x)"

## ## this would NOT be OK
## c(1, 2, 6, 300) %e% "(1, 5) | [6, max(mtcars$mpg))"

Regular Expressions

Sometimes you want to pattern match. For example, you might want to find all variable names that match a certain pattern. The %grepl% operator can help here, built offR'sgrepl` function.

## sample dataset
data <- data.frame(
  ID = c(1, 2, 3),
  cesd_1 = c(4, 5, 6),
  cesd_2 = c(7, 8, 9),
  cesd_total = c(11, 13, 15)
)

## find all variables that start with "cesd"
names(data)[grepl("^cesd", names(data))]

## or equivalently using grep() with right options
grep("^cesd", names(data), value = TRUE)

## here is the operator version
names(data) %sgrepl% "^cesd"

## the operator opens up all standard variations
names(data) %?grepl% "^cesd" ## indices
names(data) %s!grepl% "^cesd" ## subset names not in pattern
names(data) %agrepl% "^cesd" ## do all match the pattern?

An example use case in practice might be to find all variables that are items in a scale and then calculate the scale score.

set.seed(123) # Set seed for reproducibility
data <- data.frame(matrix(sample(0:4, 100, replace = TRUE), ncol = 10))
names(data) <- paste0("cesd_", 1:10)
data <- cbind(ID = 1:10, data)

## find all variables that start with "cesd" and end with a number
## use these to sum all the items for the scale score
data$cesd_total <- rowSums(data[, names(data) %sgrepl% "^cesd.*[0-9]$"])

summary(data$cesd_total)

Another example is you load a larger dataset from someone and want to check whether there are any variable names including spaces. You want to check that any space, in regular expression captured by "\s", is not contained in any variable name. That is all are not in space.

names(mtcars) %a!grepl% "\\s"

This returns TRUE indicating no variable names have a space in them.

Here is an example that fails. At least one variable name has a space. We follow it up by finding out which variable(s). This could be fed back to the data owners to change, if desired. Lastly, although these are written as separate codes, when used interactively, one might start with the test and if it fails change the a! to s to find the variables. The intention being just to slightly ease and speed up the process.

data <- data.frame(
  a = 1:4,
  `b c` = 5:8, check.names = FALSE
)

names(data) %a!grepl% "\\s" # this fails

# which variables have spaces?
names(data) %sgrepl% "\\s" 


JWiley/extraoperators documentation built on Jan. 27, 2024, 9:39 a.m.