mutate, filter, and select (dplyr) (R)

mutate adds or changes columns, filter keeps rows matching a condition, and select chooses columns. These three dplyr verbs handle the most common data-frame transformations.

R

These are core dplyr verbs, each taking a data frame and returning a modified one so they chain with the pipe. filter() keeps only rows where a logical condition is TRUE (filter(df, age > 18)). select() picks or drops columns by name (select(df, name, age) or select(df, -id)). mutate() creates new columns or overwrites existing ones from expressions of other columns (mutate(df, bmi = weight / height^2)).

They read declaratively and combine naturally: df %>% filter(score > 0) %>% mutate(grade = score / 10) %>% select(name, grade). Because mutate works on whole columns, the expressions are vectorized over rows. Inside group_by, mutate computes per group.

Together with arrange (sort), summarise (aggregate), and group_by, these verbs form dplyr's grammar of data manipulation — the clearer, modern alternative to base-R bracket subsetting.

Syntax

df %>% filter(x > 0) %>% mutate(y = x * 2) %>% select(x, y)

Examples

R — Keeps efficient cars, adds a km-per-litre column, selects two columns.

library(dplyr)
mtcars %>%
  filter(mpg > 25) %>%
  mutate(kpl = mpg * 0.425) %>%
  select(mpg, kpl) %>%
  head(2)

See also

Back to the Dictionary