-
Notifications
You must be signed in to change notification settings - Fork 3
/
group_by.R
60 lines (46 loc) · 1.26 KB
/
group_by.R
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
#' @title Group by one or more variables
#' @description Mimics dplyr::group_by using base R and rlang
#' @param .data data.frame
#' @param ... Variables to group by
#' @param add When add = FALSE, the default, group_by() will override existing groups.
#' To add to the existing groups, use add = TRUE., Default: FALSE
#' @return data.frame
#' @rdname b_group_by
#' @export
b_group_by <- function(.data,..., add = FALSE){
UseMethod("b_group_by")
}
#' @rdname b_group_by
#' @export
#' @importFrom rlang quos
b_group_by.data.frame <- function(.data,..., add = FALSE){
attr(.data,'splitby') <- rlang::quos(...)
class(.data) <- c('split_df',class(.data))
.data
}
regroup <- function(new,old){
attr(new,'splitby') <- lapply(attr(old,'splitby'),rlang::quo_set_env,parent.frame())
class(new) <- class(old)
new
}
#' @title Remove grouping from data.frame
#' @description Mimics dplyr::ungroup using base R
#' @param .data data.frame
#' @return data.frame
#' @rdname b_ungroup
#' @export
b_ungroup <- function(.data){
UseMethod("b_ungroup")
}
#' @rdname b_ungroup
#' @export
b_ungroup.default <- function(.data){
.data
}
#' @rdname b_ungroup
#' @export
b_ungroup.split_df <- function(.data){
attr(.data,'splitby') <- NULL
class(.data) <- class(.data)[-1]
.data
}