forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_data.R
144 lines (131 loc) · 3.47 KB
/
group_data.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#' Grouping metadata
#'
#' @description
#' * `group_data()` returns a data frame that defines the grouping structure.
#' The columns give the values of the grouping variables. The last column,
#' always called `.rows`, is a list of integer vectors that gives the
#' location of the rows in each group. You can retrieve just the grouping
#' data with `group_data()`, and just the locations with `group_rows()`.
#'
#' * `group_indices()` returns an integer vector the same length as `.data`
#' that gives the group that each row belongs to (cf. `group_rows()` which
#' returns the rows which each group contains).
#'
#' * `group_vars()` gives names of grouping variables as character vector;
#' `groups()` gives the names as a list of symbols.
#'
#' * `group_size()` gives the size of each group, and `n_groups()` gives the
#' total number of groups.
#'
#' See [context] for equivalent functions that return values for the _current_
#' group.
#' @param .data,.tbl,x A data frame or extension (like a tibble or grouped
#' tibble).
#' @param ... Use of `...` is now deprecated; please use `group_by()` first
#' instead.
#' @keywords internal
#' @examples
#' df <- tibble(x = c(1,1,2,2))
#' group_vars(df)
#' group_rows(df)
#' group_data(df)
#'
#' gf <- group_by(df, x)
#' group_vars(gf)
#' group_rows(gf)
#' group_data(gf)
#' @export
group_data <- function(.data) {
UseMethod("group_data")
}
#' @export
group_data.data.frame <- function(.data) {
out <- vec_init(.data[0], 1)
rownames(out) <- NULL
out$.rows <- list_of(seq_len(nrow(.data)), .ptype = integer())
out
}
#' @export
group_data.rowwise_df <- function(.data) {
attr(.data, "groups")
}
#' @export
group_data.grouped_df <- function(.data) {
attr(validate_grouped_df(.data), "groups")
}
# -------------------------------------------------------------------------
#' @rdname group_data
#' @export
group_keys <- function(.tbl, ...) {
UseMethod("group_keys")
}
#' @export
group_keys.data.frame <- function(.tbl, ...) {
if (dots_n(...) > 0) {
lifecycle::deprecate_warn(
"1.0.0", "group_keys(... = )",
details = "Please `group_by()` first"
)
.tbl <- group_by(.tbl, ...)
}
out <- group_data(.tbl)
attr(out, ".drop") <- NULL
out[-length(out)]
}
#' @rdname group_data
#' @export
group_rows <- function(.data) {
group_data(.data)[[".rows"]]
}
#' @export
#' @rdname group_data
group_indices <- function(.data, ...) {
if (nargs() == 0) {
lifecycle::deprecate_warn("1.0.0", "group_indices()", "cur_group_id()")
return(cur_group_id())
}
UseMethod("group_indices")
}
#' @export
group_indices.data.frame <- function(.data, ...) {
if (dots_n(...) > 0) {
lifecycle::deprecate_warn(
"1.0.0", "group_keys(... = )",
details = "Please `group_by()` first"
)
.data <- group_by(.data, ...)
}
.Call(`dplyr_group_indices`, group_rows(.data), nrow(.data))
}
#' @export
#' @rdname group_data
group_vars <- function(x) {
UseMethod("group_vars")
}
#' @export
group_vars.data.frame <- function(x) {
setdiff(names(group_data(x)), ".rows")
}
#' @export
#' @rdname group_data
groups <- function(x) {
UseMethod("groups")
}
#' @export
groups.data.frame <- function(x) {
syms(group_vars(x))
}
#' @export
#' @rdname group_data
group_size <- function(x) UseMethod("group_size")
#' @export
group_size.data.frame <- function(x) {
lengths(group_rows(x))
}
#' @export
#' @rdname group_data
n_groups <- function(x) UseMethod("n_groups")
#' @export
n_groups.data.frame <- function(x) {
nrow(group_data(x))
}