typestar

filter in R

Keeping rows, with conditions combined by comma or by operator.

library(dplyr)

runs <- tibble(
  lang = c("r", "python", "sql", "css", NA),
  tpm = c(96, 104, 79, 70, 88)
)

print(filter(runs, tpm > 80, lang != "sql"))
print(filter(runs, tpm > 100 | lang == "css"))
print(filter(runs, between(tpm, 75, 97)))
print(filter(runs, lang %in% c("r", "sql")))
print(filter(runs, is.na(lang)))
print(nrow(filter(runs, lang != "r")))

How it works

  1. Commas mean and; use | for or.
  2. between and %in% replace longer comparisons.
  3. filter drops NA rows for the tested column, which surprises people.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
357
Tokens
135
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 85 seconds.

Type this snippet

Step 1 of 5 in Rows, step 5 of 27 in dplyr.

← Previous Next →