forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouped-df.r
326 lines (285 loc) · 8.63 KB
/
grouped-df.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#' A grouped data frame.
#'
#' @description
#' The easiest way to create a grouped data frame is to call the `group_by()`
#' method on a data frame or tbl: this will take care of capturing
#' the unevaluated expressions for you.
#'
#' See [group_data()] for the accessor functions that retrieve various metadata
#' from a `grouped_df`.
#'
#' @keywords internal
#' @param data a tbl or data frame.
#' @param vars A character vector.
#' @param drop When `.drop = TRUE`, empty groups are dropped.
#'
#' @import vctrs
#' @importFrom zeallot %<-%
#'
#' @export
grouped_df <- function(data, vars, drop = FALSE) {
if (!is.data.frame(data)) {
abort("`data` must be a data frame")
}
if (!is.character(vars)) {
abort("`vars` must be a character vector")
}
if (length(vars) == 0) {
as_tibble(data)
} else {
groups <- compute_groups(data, vars, drop = drop)
new_grouped_df(data, groups)
}
}
compute_groups <- function(data, vars, drop = FALSE) {
unknown <- setdiff(vars, names(data))
if (length(unknown) > 0) {
vars <- paste0(encodeString(vars, quote = "`"), collapse = ", ")
abort(glue("`vars` missing from `data`: {vars}"))
}
for (var in vars) {
x <- data[[var]]
if (is.factor(x) && anyNA(x)) {
warn(glue("Factor `{var}` contains implicit NA, consider using `forcats::fct_explicit_na()`"))
}
}
# Only train the dictionary based on selected columns
group_vars <- as_tibble(data)[vars]
c(old_keys, old_rows) %<-% vec_split_id_order(group_vars)
signal("", class = "dplyr_regroup")
groups <- tibble(!!!old_keys, .rows := old_rows)
if (!isTRUE(drop) && any(map_lgl(old_keys, is.factor))) {
# Extra work is needed to auto expand empty groups
uniques <- map(old_keys, function(.) {
if (is.factor(.)) . else vec_unique(.)
})
# Internally we only work with integers
#
# so for any grouping column that is not a factor
# we need to match the values to the unique values
positions <- map2(old_keys, uniques, function(.x, .y) {
if (is.factor(.x)) .x else vec_match(.x, .y)
})
# Expand groups internally adds empty groups recursively
# we get back:
# - indices: a list of how to vec_slice the current keys
# to get the new keys
#
# - rows: the new list of rows (i.e. the same as old rows,
# but with some extra empty integer(0) added for empty groups)
c(new_indices, new_rows) %<-% expand_groups(groups, positions, vec_size(old_keys))
# Make the new keys from the old keys and the new_indices
new_keys <- pmap(list(old_keys, new_indices, uniques), function(key, index, unique) {
if(is.factor(key)) {
new_factor(index, levels = levels(key))
} else {
vec_slice(unique, index)
}
})
names(new_keys) <- vars
groups <- tibble(!!!new_keys, .rows := new_rows)
}
structure(groups, .drop = drop)
}
count_regroups <- function(code) {
i <- 0
withCallingHandlers(code, dplyr_regroup = function(cnd) {
i <<- i + 1
})
i
}
show_regroups <- function(code) {
withCallingHandlers(code, dplyr_regroup = function(cnd) {
cat("Regrouping...\n")
})
}
#' Low-level construction and validation for the grouped_df class
#'
#' `new_grouped_df()` is a constructor designed to be high-performance so only
#' check types, not values. This means it is the caller's responsibility
#' to create valid values, and hence this is for expert use only.
#'
#' @param x A data frame
#' @param groups The grouped structure, `groups` should be a data frame.
#' Its last column should be called `.rows` and be
#' a list of 1 based integer vectors that all are between 1 and the number of rows of `.data`.
#' @param class additional class, will be prepended to canonical classes of a grouped data frame.
#' @param check_bounds whether to check all indices for out of bounds problems in grouped_df objects
#' @param ... additional attributes
#'
#' @examples
#' # 5 bootstrap samples
#' tbl <- new_grouped_df(
#' tibble(x = rnorm(10)),
#' groups = tibble(".rows" := replicate(5, sample(1:10, replace = TRUE), simplify = FALSE))
#' )
#' # mean of each bootstrap sample
#' summarise(tbl, x = mean(x))
#'
#' @importFrom tibble new_tibble
#' @keywords internal
#' @export
new_grouped_df <- function(x, groups, ..., class = character()) {
if (!is.data.frame(x)) {
abort(c(
"`new_grouped_df()` incompatible argument",
"`x` is not a data frame")
)
}
if (!is.data.frame(groups) || tail(names(groups), 1L) != ".rows") {
abort(c(
"`new_grouped_df()` incompatible argument",
"`groups` should be a data frame, and its last column be called `.rows`"
))
}
new_tibble(
x,
groups = groups,
...,
nrow = NROW(x),
class = c(class, "grouped_df")
)
}
#' @description
#' `validate_grouped_df()` validates the attributes of a `grouped_df`.
#'
#' @rdname new_grouped_df
#' @export
validate_grouped_df <- function(x, check_bounds = FALSE) {
result <- .Call(`dplyr_validate_grouped_df`, x, nrow(x), check_bounds)
if (!is.null(result)) {
abort(result, class = "dplyr_grouped_df_corrupt")
}
x
}
setOldClass(c("grouped_df", "tbl_df", "tbl", "data.frame"))
#' @rdname grouped_df
#' @export
is.grouped_df <- function(x) inherits(x, "grouped_df")
#' @rdname grouped_df
#' @export
is_grouped_df <- is.grouped_df
group_sum <- function(x) {
grps <- n_groups(x)
paste0(commas(group_vars(x)), " [", big_mark(grps), "]")
}
#' @export
tbl_sum.grouped_df <- function(x) {
c(
NextMethod(),
c("Groups" = group_sum(x))
)
}
#' @export
as.data.frame.grouped_df <- function(x, row.names = NULL,
optional = FALSE, ...) {
new_data_frame(vec_data(x), n = nrow(x))
}
#' @export
as_tibble.grouped_df <- function(x, ...) {
new_tibble(vec_data(x), nrow = nrow(x))
}
#' @importFrom tibble is_tibble
#' @export
`[.grouped_df` <- function(x, i, j, drop = FALSE) {
out <- NextMethod()
if (!is.data.frame(out)) {
return(out)
}
if (drop) {
as_tibble(out)
} else {
groups <- intersect(names(out), group_vars(x))
if ((missing(i) || nargs() == 2) && identical(groups, group_vars(x))) {
new_grouped_df(out, group_data(x))
} else {
grouped_df(out, groups, group_by_drop_default(x))
}
}
}
#' @export
`$<-.grouped_df` <- function(x, name, ..., value) {
out <- NextMethod()
if (name %in% group_vars(x)) {
grouped_df(out, intersect(names(out), group_vars(x)), group_by_drop_default(x))
} else {
out
}
}
#' @export
`[<-.grouped_df` <- function(x, i, j, ..., value) {
out <- NextMethod()
grouped_df(out, intersect(names(out), group_vars(x)), group_by_drop_default(x))
}
#' @export
`[[<-.grouped_df` <- function(x, ..., value) {
out <- NextMethod()
grouped_df(out, intersect(names(out), group_vars(x)), group_by_drop_default(x))
}
#' @export
`names<-.grouped_df` <- function(x, value) {
data <- as.data.frame(x)
names(data) <- value
groups <- group_data(x)
group_loc <- match(intersect(names(x), names(groups)), names(x))
group_names <- c(value[group_loc], ".rows")
if (!identical(group_names, names(groups))) {
names(groups) <- c(value[group_loc], ".rows")
}
new_grouped_df(data, groups)
}
#' @method rbind grouped_df
#' @export
rbind.grouped_df <- function(...) {
bind_rows(...)
}
#' @method cbind grouped_df
#' @export
cbind.grouped_df <- function(...) {
bind_cols(...)
}
#' Select grouping variables
#'
#' This selection helpers matches grouping variables. It can be used
#' in [select()] or [vars()][scoped] selections.
#'
#' @inheritParams tidyselect::select_helpers
#' @seealso [groups()] and [group_vars()] for retrieving the grouping
#' variables outside selection contexts.
#'
#' @examples
#' gdf <- iris %>% group_by(Species)
#'
#' # Select the grouping variables:
#' gdf %>% select(group_cols())
#'
#' # Remove the grouping variables from mutate selections:
#' gdf %>% mutate_at(vars(-group_cols()), `/`, 100)
#' @export
group_cols <- function(vars = peek_vars()) {
if (is_sel_vars(vars)) {
matches <- match(vars %@% groups, vars)
if (anyNA(matches)) {
abort("Can't find the grouping variables")
}
matches
} else {
int()
}
}
group_data_trim <- function(group_data, preserve = FALSE) {
if (preserve) {
return(group_data)
}
non_empty <- lengths(group_data$".rows") > 0
group_data[non_empty, , drop = FALSE]
}
# Helpers -----------------------------------------------------------------
expand_groups <- function(old_groups, positions, nr) {
.Call(`dplyr_expand_groups`, old_groups, positions, nr)
}
vec_split_id_order <- function(x) {
split_id <- vec_group_loc(x)
split_id$loc <- new_list_of(split_id$loc, ptype = integer())
vec_slice(split_id, vec_order(split_id$key))
}