'[' - Extract or Replace

  • '[' takes both numeric and logical vectors
  • Therefore, we can index with an expression or the which() function
x <- runif(10, 1, 10)
y <- runif(10, 1, 10)

# These are the same
all.equal(x[x>y], # indexer returns logical vector
         x[which(x>y)]) # indexer returns numeric vector
## [1] TRUE