typestar

filter and select in R

dplyr's verbs for choosing rows and columns.

library(dplyr)

light <- filter(mtcars, wt < 3, mpg > 20)
either <- filter(mtcars, cyl == 4 | hp > 200)

three_cols <- select(mtcars, mpg, cyl, wt)
dropped <- select(mtcars, -disp, -drat)
by_pattern <- select(mtcars, starts_with("d"))

renamed <- rename(mtcars, miles_per_gallon = mpg)
sliced <- slice(mtcars, 1:5)

How it works

  1. filter keeps rows; commas mean AND, | means OR.
  2. select keeps or drops columns, or matches a pattern.
  3. rename and slice adjust names and row positions.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
314
Tokens
88
Three-star pace
95 tpm

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

Type this snippet

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

← Previous Next →