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
- Commas mean and; use
|for or. betweenand%in%replace longer comparisons.filterdrops NA rows for the tested column, which surprises people.
Keywords and builtins used here
betweencfilterlibrarynrowprinttibble
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.
Step 1 of 5 in Rows, step 5 of 27 in dplyr.