Learn R Programming

statar (version 0.1.3)

setmutate: Version of mutate that (i) transforms data.table in place (ii) allows by, i condition

Description

Version of mutate that (i) transforms data.table in place (ii) allows by, i condition

Usage

setmutate(x, ..., i = NULL, by = NULL)

setmutate_(x, vars, i = NULL, by = NULL)

Arguments

x
a data.table
...
Variables to include. Defaults to all non-grouping variables.
i
a condition that restricts the set of row on which to apply the function
by
Groups within which the function should be applied
vars
Used to work around non-standard evaluation.

Examples

Run this code
library(data.table)
library(dplyr)
N <- 100
DT <- data.table(
  id = sample(5, N, TRUE),
  v1 = sample(5, N, TRUE),
  v2 = sample(1e6, N, TRUE)
)
setmutate(DT, mean(v2), by = v1)
setmutate_(DT, setNames("mean(v2)", "v3"), by = "v1")
setmutate_(DT, setNames("mean(v2)", "v3"), i = "id==1", by = "v1")

Run the code above in your browser using DataLab