group_dt: Data manipulation within groups

Description Usage Arguments Value Examples

View source: R/group_dt.R

Description

Analogous function for group_by in dplyr, but in another efficient way.

Usage

1

Arguments

data

data.frame

by

Variables to group by,unquoted name of grouping variable of list of unquoted names of grouping variables.

...

Any data manipulation arguments that could be implemented on a data.frame.

Value

data.table

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
iris %>% group_dt(by = Species,slice_dt(1:2))
iris %>% group_dt(Species,filter_dt(Sepal.Length == max(Sepal.Length)))
iris %>% group_dt(Species,summarise_dt(new = max(Sepal.Length)))

# you can pipe in the `group_dt`
iris %>% group_dt(Species,
                  mutate_dt(max= max(Sepal.Length)) %>%
                    summarise_dt(sum=sum(Sepal.Length)))

# for summarise_dt, you can use "by" to calculate within the group
mtcars %>%
  summarise_dt(
   disp = mean(disp),
   hp = mean(hp),
   by = cyl
)

 # but you could also, of course, use group_dt
mtcars %>%
  group_dt(by =.(vs,am),
    summarise_dt(avg = mean(mpg)))

hope-data-science/tidydt documentation built on Feb. 21, 2020, 10:25 a.m.