mutate.: Add/modify/delete columns

Description Usage Arguments Examples

View source: R/mutate.R

Description

With mutate.() you can do 3 things:

Usage

1
mutate.(.df, ..., .by = NULL, .keep = "all", .before = NULL, .after = NULL)

Arguments

.df

A data.frame or data.table

...

Columns to add/modify

.by

Columns to group by

.keep

experimental: This is an experimental argument that allows you to control which columns from .df are retained in the output:

  • "all", the default, retains all variables.

  • "used" keeps any variables used to make new variables; it's useful for checking your work as it displays inputs and outputs side-by-side.

  • "unused" keeps only existing variables not used to make new variables.

  • "none", only keeps grouping keys (like transmute.()).

.before, .after

Optionally indicate where new columns should be placed. Defaults to the right side of the data frame.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
test_df <- data.table(
  a = 1:3,
  b = 4:6,
  c = c("a","a","b")
)

test_df %>%
  mutate.(double_a = a * 2,
          a_plus_b = a + b)

test_df %>%
  mutate.(double_a = a * 2,
          avg_a = mean(a),
          .by = c)

test_df %>%
  mutate.(double_a = a * 2, .keep = "used")

test_df %>%
  mutate.(double_a = a * 2, .after = a)

mtfairbanks/gdt documentation built on May 4, 2021, 9:44 a.m.