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

## ----setup--------------------------------------------------------------------
library(extraoperators)

## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

## base R: are any numbers in 50 or 99?
any(sample_numbers %in% c(50, 99))

## extraoperators: are any numbers in 50 or 99?
sample_numbers %anyin% c(50, 99)

## extraoperators: are any numbers between 90 and 100?
sample_numbers %anygl% c(90, 100)

## base any() NA handling is preserved
c(NA, 1) %anyg% 2

## flip order around a colon before matching
c("a:b", "c:d") %anyflipIn% "b:a"


## -----------------------------------------------------------------------------

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


## -----------------------------------------------------------------------------

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

## extraoperators: are objects strictly identical?
sample_numbers %===% sample_numbers

## identical() is type strict
1 %===% 1L
1 %!==% 1L


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

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

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

## -----------------------------------------------------------------------------

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



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


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

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

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

