Package provides pipe-style interface for data.table
. It preserves
all data.table features without significant impact on performance. 'let
'
and 'take
' functions are simplified interfaces for most common data
manipulation tasks.
To select rows from data: take_if(mtcars, am==0)
To select columns from data: take(mtcars, am, vs, mpg)
To aggregate data: take(mtcars, mean_mpg = mean(mpg), by = am)
To aggregate all non-grouping columns: take(mtcars, fun = mean, by = am)
To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
To get total summary skip 'by' argument: take(mtcars, fun = mean)
Use magrittr pipe '%>%' to chain several operations:
mtcars %>% let(mpg_hp = mpg/hp) %>% take(mean(mpg_hp), by = am)
To modify variables or add new variables:
mtcars %>% let(new_var = 42, new_var2 = new_var*hp) %>% head()
To drop variable assign NULL: let(mtcars, am = NULL) %>% head()
For parametric assignment use ':=':
new_var = "my_var" old_var = "mpg" mtcars %>% let((new_var) := get(old_var)*2) %>% head()
For more sophisticated operations see 'query'/'query_if': these
functions translates its arguments one-to-one to '[.data.table
'
method. Additionally there are some conveniences such as automatic
'data.frame' conversion to 'data.table'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | # examples form 'dplyr' package
data(mtcars)
# Newly created variables are available immediately
mtcars %>%
let(
cyl2 = cyl * 2,
cyl4 = cyl2 * 2
) %>% head()
# You can also use let() to remove variables and
# modify existing variables
mtcars %>%
let(
mpg = NULL,
disp = disp * 0.0163871 # convert to litres
) %>% head()
# window functions are useful for grouped computations
mtcars %>%
let(rank = rank(-mpg, ties.method = "min"),
by = cyl) %>%
head()
# You can drop variables by setting them to NULL
mtcars %>%
let(cyl = NULL) %>%
head()
# keeps all existing variables
mtcars %>%
let(displ_l = disp / 61.0237) %>%
head()
# keeps only the variables you create
mtcars %>%
take(displ_l = disp / 61.0237)
# can refer to both contextual variables and variable names:
var = 100
mtcars %>%
let(cyl = cyl * var) %>%
head()
# filter by condition
mtcars %>%
take_if(am==0)
# filter by compound condition
mtcars %>%
take_if(am==0 & mpg>mean(mpg))
# A 'take' with summary functions applied without 'by' argument returns an aggregated data
mtcars %>%
take(mean = mean(disp), n = .N)
# Usually, you'll want to group first
mtcars %>%
take(mean = mean(disp), n = .N, by = am)
# grouping by multiple variables
mtcars %>%
take(mean = mean(disp), n = .N, by = list(am, vs))
# parametric evaluation:
var = quote(mean(cyl))
take(mtcars, eval(var))
# You can group by expressions:
mtcars %>%
take(
fun = mean,
by = list(vsam = vs + am)
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.