/
one-stage-verbs.R
325 lines (264 loc) · 7.96 KB
/
one-stage-verbs.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
#' One Stage function
#' @param x the input
#' @param listx a list
#' @param na.rm Remove NAs. TRUE of FALSE
#' @param ... additional options
#' @rdname one-stage-group-by-verbs
#' @export
var_df.chunk_agg.disk.frame <- function(x, na.rm = FALSE) {
# Guard against Github #241
data.frame(
sumx = sum(x, na.rm = na.rm),
sumsqrx = sum(x^2, na.rm = na.rm),
nx = length(x) - ifelse(na.rm, sum(is.na(x)), 0)
)
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom dplyr bind_rows
var_df.collected_agg.disk.frame <- function(listx) {
df = Reduce(dplyr::bind_rows, listx)
sumlengthx = sum(df$nx)
first_part = sum(df$sumsqrx) / sumlengthx
second_part = sum(df$sumx) / sumlengthx
# unbiased adjustment
(first_part - second_part^2) * sumlengthx / (sumlengthx-1)
}
#' @export
#' @rdname one-stage-group-by-verbs
sd_df.chunk_agg.disk.frame <- var_df.chunk_agg.disk.frame
#' @export
#' @rdname one-stage-group-by-verbs
sd_df.collected_agg.disk.frame <- function(listx) {
sqrt(var_df.collected_agg.disk.frame(listx))
}
#' mean chunk_agg
#' @export
#' @rdname one-stage-group-by-verbs
mean_df.chunk_agg.disk.frame <- function(x, na.rm = FALSE, ...) {
sumx = sum(x, na.rm = na.rm)
lengthx = length(x) - ifelse(na.rm, sum(is.na(x)), 0)
data.frame(sumx = sumx, lengthx = lengthx)
}
#' mean collected_agg
#' @export
#' @rdname one-stage-group-by-verbs
mean_df.collected_agg.disk.frame <- function(listx) {
sum(sapply(listx, function(x) x$sumx))/sum(sapply(listx, function(x) x$lengthx))
}
#' @export
#' @rdname one-stage-group-by-verbs
sum_df.chunk_agg.disk.frame <- function(x, ...) {
sum(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
sum_df.collected_agg.disk.frame <- function(listx, ...) {
sum(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
min_df.chunk_agg.disk.frame <- function(x, ...) {
min(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
min_df.collected_agg.disk.frame <- function(listx, ...) {
min(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
max_df.chunk_agg.disk.frame <- function(x, ...) {
max(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
max_df.collected_agg.disk.frame <- function(listx, ...) {
max(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom stats median
median_df.chunk_agg.disk.frame <- function(x, ...) {
stats::median(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
median_df.collected_agg.disk.frame <- function(listx, ...) {
stats::median(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom dplyr n
n_df.chunk_agg.disk.frame <- function(...) {
dplyr::n()
}
#' @export
#' @rdname one-stage-group-by-verbs
n_df.collected_agg.disk.frame <- function(listx, ...) {
sum(unlist(listx))
}
#' @export
#' @rdname one-stage-group-by-verbs
length_df.chunk_agg.disk.frame <- function(x, ...) {
length(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
length_df.collected_agg.disk.frame <- function(listx, ...) {
length(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
any_df.chunk_agg.disk.frame <- function(x, ...) {
any(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
any_df.collected_agg.disk.frame <- function(listx, ...) {
any(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
all_df.chunk_agg.disk.frame <- function(x, ...) {
all(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
all_df.collected_agg.disk.frame <- function(listx, ...) {
all(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
n_distinct_df.chunk_agg.disk.frame <- function(x, na.rm = FALSE, ...) {
if(na.rm) {
setdiff(unique(x), NA)
} else {
unique(x)
}
}
#' @export
#' @importFrom dplyr n_distinct
#' @rdname one-stage-group-by-verbs
n_distinct_df.collected_agg.disk.frame <- function(listx, ...) {
n_distinct(unlist(listx))
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom stats quantile
quantile_df.chunk_agg.disk.frame <- function(x, ...) {
stats::quantile(x, ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom stats quantile
quantile_df.collected_agg.disk.frame <- function(listx, ...) {
stats::quantile(unlist(listx), ...)
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom stats quantile
IQR_df.chunk_agg.disk.frame <- function(x, na.rm = FALSE, ...) {
stats::quantile(x, c(0.25, 0.75), na.rm = na.rm)
#100
}
#' @export
#' @rdname one-stage-group-by-verbs
#' @importFrom stats quantile
IQR_df.collected_agg.disk.frame <- function(listx, ...) {
q25 = unlist(listx)[c(TRUE, FALSE)]
q75 = unlist(listx)[c(FALSE, TRUE)]
quantile(q75, 0.75) - quantile(q25, 0.25)
}
#' A function to parse the summarize function
#' @importFrom dplyr filter select pull
#' @importFrom purrr map_dfr
#' @rdname group_by
#' @export
summarise.grouped_disk.frame <- function(.data, ...) {
class(.data) <- c("summarized_disk.frame", "disk.frame")
# get all components of the summarise
dotdotdot = rlang::enexprs(...)
# convert any quosure to labels
for (i in seq_along(dotdotdot)) {
dotdotdot[[i]] <- rlang::quo_squash(dotdotdot[[i]])
}
attr(.data, "summarize_code") = dotdotdot
# detect any global variables
args_str = sapply(dotdotdot, function(code) {
deparse(code) %>%
paste0(collapse="")
}) %>% paste(collapse = ", ")
attr(.data, "summarize_globals_and_pkgs") =
find_globals_recursively(
parse(text=sprintf("list(%s)", args_str))[[1]],
parent.frame()
)
return(.data)
}
#' @export
#' @rdname group_by
summarize.grouped_disk.frame = summarise.grouped_disk.frame
#' Group by within each disk.frame
#' @description
#' The disk.frame group by operation perform group WITHIN each chunk. This is
#' often used for performance reasons. If the user wishes to perform group-by,
#' they may choose to use the `hard_group_by` function which is expensive as it
#' reorganizes the chunks by the shard key.
#' @seealso hard_group_by
#' @param .data a disk.frame
#' @param .add from dplyr
#' @param .drop from dplyr
#' @param ... same as the dplyr::group_by
#' @importFrom dplyr group_by_drop_default
#' @importFrom rlang enexpr
#' @export
#' @rdname group_by
# learning from https://docs.dask.org/en/latest/dataframe-groupby.html
group_by.disk.frame <- function(.data, ..., .add = FALSE, .drop = stop("disk.frame does not support `.drop` in `group_by` at this stage")) {
class(.data) <- c("grouped_disk.frame", "disk.frame")
# using rlang is a necessary evil here as I need to deal with !!! that is supported by group_by etc
group_by_cols = rlang::enexprs(...)
# convert any quosure to labels
for (i in seq_along(group_by_cols)) {
group_by_cols[[i]] <- group_by_cols[[i]] %>%
rlang::quo_squash()
}
attr(.data, "group_by_cols") = group_by_cols
# detect any global variables
args_str = sapply(group_by_cols, function(code) {
deparse(code) %>%
paste0(collapse="")
}) %>% paste(collapse = ", ")
attr(.data, "group_by_globals_and_pkgs") = find_globals_recursively(parse(text=sprintf("list(%s)", args_str))[[1]], parent.frame())
.data
}
#' @export
#' @importFrom dplyr summarize
#' @rdname group_by
summarize.disk.frame <- function(.data, ...) {
class(.data) <- c("summarized_disk.frame", "disk.frame")
# get all components of the summarise
dotdotdot = rlang::enexprs(...)
# convert any quosure to labels
for (i in seq_along(dotdotdot)) {
dotdotdot[[i]] <- dotdotdot[[i]] %>%
rlang::quo_squash()
}
attr(.data, "summarize_code") = dotdotdot
# detect any global variables
args_str = sapply(dotdotdot, function(code) {
deparse(code) %>%
paste0(collapse="")
}) %>% paste(collapse = ", ")
attr(.data, "summarize_globals_and_pkgs") =
find_globals_recursively(
parse(text=sprintf("list(%s)", args_str))[[1]],
parent.frame()
)
return(.data)
}
#' @export
#' @importFrom dplyr summarize
#' @rdname group_by
summarise.disk.frame <- summarize.disk.frame