Vectorized filters express the whole selection rule at once instead of one item at a time.

Program

Play the script to change the threshold and see how many values are kept.

threshold
vector_filter.R
Replay: real traced execution (multi-file project)
threshold <- 10
values <- c(4, 9, 12, 18)
kept <- values[values >= threshold]
count <- length(kept)
label <- paste("kept", count, sep = ":")
cat(label, "\n", sep = "")
threshold <- 5
values <- c(4, 9, 12, 18)
kept <- values[values >= threshold]
count <- length(kept)
label <- paste("kept", count, sep = ":")
cat(label, "\n", sep = "")
threshold <- 15
values <- c(4, 9, 12, 18)
kept <- values[values >= threshold]
count <- length(kept)
label <- paste("kept", count, sep = ":")
cat(label, "\n", sep = "")
  1. threshold ← 10

    1threshold <- 102values <- c(4, 9, 12, 18)
    values this step10threshold
  2. values ← 4, 9, 12, 18

    1threshold <- 102values <- c(4, 9, 12, 18)3kept <- values[values >= threshold]
    values this step4, 9, 12, 18values
  3. kept ← 12, 18

    2values <- c(4, 9, 12, 18)3kept <- values[values >= threshold]4count <- length(kept)
    values this step12, 18kept4, 9, 12, 18values10threshold
  4. count ← 2

    3kept <- values[values >= threshold]4count <- length(kept)5label <- paste("kept", count, sep = ":")
    values this step2count12, 18kept
  5. label ← kept:2

    4count <- length(kept)5label <- paste("kept", count, sep = ":")6cat(label, "\n", sep = "")
    values this stepkept:2label2count
  6. cat(label, " ", sep = "")

    5label <- paste("kept", count, sep = ":")6cat(label, "\n", sep = "")
    outputkept:2
    values this stepkept:2label
  1. threshold ← 5

    1threshold <- 52values <- c(4, 9, 12, 18)
    values this step5threshold
  2. values ← 4, 9, 12, 18

    1threshold <- 52values <- c(4, 9, 12, 18)3kept <- values[values >= threshold]
    values this step4, 9, 12, 18values
  3. kept ← 9, 12, 18

    2values <- c(4, 9, 12, 18)3kept <- values[values >= threshold]4count <- length(kept)
    values this step9, 12, 18kept4, 9, 12, 18values5threshold
  4. count ← 3

    3kept <- values[values >= threshold]4count <- length(kept)5label <- paste("kept", count, sep = ":")
    values this step3count9, 12, 18kept
  5. label ← kept:3

    4count <- length(kept)5label <- paste("kept", count, sep = ":")6cat(label, "\n", sep = "")
    values this stepkept:3label3count
  6. cat(label, " ", sep = "")

    5label <- paste("kept", count, sep = ":")6cat(label, "\n", sep = "")
    outputkept:3
    values this stepkept:3label
  1. threshold ← 15

    1threshold <- 152values <- c(4, 9, 12, 18)
    values this step15threshold
  2. values ← 4, 9, 12, 18

    1threshold <- 152values <- c(4, 9, 12, 18)3kept <- values[values >= threshold]
    values this step4, 9, 12, 18values
  3. kept ← 18

    2values <- c(4, 9, 12, 18)3kept <- values[values >= threshold]4count <- length(kept)
    values this step18kept4, 9, 12, 18values15threshold
  4. count ← 1

    3kept <- values[values >= threshold]4count <- length(kept)5label <- paste("kept", count, sep = ":")
    values this step1count18kept
  5. label ← kept:1

    4count <- length(kept)5label <- paste("kept", count, sep = ":")6cat(label, "\n", sep = "")
    values this stepkept:1label1count
  6. cat(label, " ", sep = "")

    5label <- paste("kept", count, sep = ":")6cat(label, "\n", sep = "")
    outputkept:1
    values this stepkept:1label
vectorized rule `values >= threshold` creates one logical decision per value.
subset The bracket expression keeps all matching values in one statement.
work summary Counting kept values summarizes how much data remains.