forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolwise.R
299 lines (264 loc) · 9.15 KB
/
colwise.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
#' Summarise and mutate multiple columns.
#'
#' \code{summarise_all()} and \code{mutate_all()} apply the functions
#' to all (non-grouping) columns. \code{summarise_at()} and
#' \code{mutate_at()} allow you to select columns
#' using the same name-based \code{\link{select_helpers}} as with
#' \code{\link{select}()}. \code{summarise_if}() and
#' \code{mutate_if}() operate on columns for which a predicate returns
#' \code{TRUE}. Finally, \code{\link{summarise_each}()} and
#' \code{\link{mutate_each}()} are older variants that will be
#' deprecated in the future.
#'
#' @param .tbl a tbl
#' @param .funs List of function calls generated by
#' \code{\link{funs}()}, or a character vector of function names, or
#' simply a function (only for local sources).
#' @param .cols A list of columns generated by \code{\link{vars}()},
#' or a character vector of column names, or a numeric vector of column
#' positions.
#' @param .predicate A predicate function to be applied to the columns
#' or a logical vector. The columns for which \code{.predicate} is
#' or returns \code{TRUE} will be summarised or mutated.
#' @param ... Additional arguments for the function calls. These are
#' evaluated only once.
#' @return A data frame. By default, the newly created columns have the shortest
#' names needed to distinguish the output. To force inclusion of a name,
#' even when not needed, name the input (see examples for details).
#' @seealso \code{\link{vars}()}, \code{\link{funs}()}
#' @examples
#' by_species <- iris %>% group_by(Species)
#'
#' # One function
#' by_species %>% summarise_all(n_distinct)
#' by_species %>% summarise_all(mean)
#'
#' # Use the _at and _if variants for conditional mapping.
#' by_species %>% summarise_if(is.numeric, mean)
#'
#' # summarise_at() can use select() helpers with the vars() function:
#' by_species %>% summarise_at(vars(Petal.Width), mean)
#' by_species %>% summarise_at(vars(matches("Width")), mean)
#'
#' # You can also specify columns with column names or column positions:
#' by_species %>% summarise_at(c("Sepal.Width", "Petal.Width"), mean)
#' by_species %>% summarise_at(c(1, 3), mean)
#'
#' # You can provide additional arguments. Those are evaluated only once:
#' by_species %>% summarise_all(mean, trim = 1)
#' by_species %>% summarise_at(vars(Petal.Width), mean, trim = 1)
#'
#' # You can provide an expression or multiple functions with the funs() helper.
#' by_species %>% mutate_all(funs(. * 0.4))
#' by_species %>% summarise_all(funs(min, max))
#' # Note that output variable name must now include function name, in order to
#' # keep things distinct.
#'
#' # Function names will be included if .funs has names or whenever multiple
#' # functions are used.
#' by_species %>% mutate_all(funs("in" = . / 2.54))
#' by_species %>% mutate_all(funs(rg = diff(range(.))))
#' by_species %>% summarise_all(funs(med = median))
#' by_species %>% summarise_all(funs(Q3 = quantile), probs = 0.75)
#' by_species %>% summarise_all(c("min", "max"))
#'
#' # Two functions, continued
#' by_species %>% summarise_at(vars(Petal.Width, Sepal.Width), funs(min, max))
#' by_species %>% summarise_at(vars(matches("Width")), funs(min, max))
#'
#' @aliases summarise_each_q mutate_each_q
#' @export
summarise_all <- function(.tbl, .funs, ...) {
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
cols <- lazyeval::lazy_dots(everything())
vars <- colwise_(.tbl, funs, cols)
summarise_(.tbl, .dots = vars)
}
#' @rdname summarise_all
#' @export
mutate_all <- function(.tbl, .funs, ...) {
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
cols <- lazyeval::lazy_dots(everything())
vars <- colwise_(.tbl, funs, cols)
mutate_(.tbl, .dots = vars)
}
#' @rdname summarise_all
#' @export
summarise_if <- function(.tbl, .predicate, .funs, ...) {
if (inherits(.tbl, "tbl_lazy")) {
stop("Conditional colwise operations currently require local sources",
call. = FALSE)
}
cols <- probe_colwise_names(.tbl, .predicate)
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
vars <- colwise_(.tbl, funs, cols)
summarise_(.tbl, .dots = vars)
}
#' @rdname summarise_all
#' @export
mutate_if <- function(.tbl, .predicate, .funs, ...) {
if (inherits(.tbl, "tbl_lazy")) {
stop("Conditional colwise operations currently require local sources",
call. = FALSE)
}
cols <- probe_colwise_names(.tbl, .predicate)
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
vars <- colwise_(.tbl, funs, cols)
mutate_(.tbl, .dots = vars)
}
probe_colwise_names <- function(tbl, p, ...) {
if (is.logical(p)) {
stopifnot(length(p) == length(tbl))
selected <- p
} else {
selected <- vapply(tbl, p, logical(1), ...)
}
vars <- tbl_vars(tbl)
vars[selected]
}
#' @rdname summarise_all
#' @export
summarise_at <- function(.tbl, .cols, .funs, ...) {
cols <- select_colwise_names(.tbl, .cols)
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
vars <- colwise_(.tbl, funs, cols)
summarise_(.tbl, .dots = vars)
}
#' @rdname summarise_all
#' @export
mutate_at <- function(.tbl, .cols, .funs, ...) {
cols <- select_colwise_names(.tbl, .cols)
funs <- as.fun_list(.funs, .env = parent.frame(), ...)
vars <- colwise_(.tbl, funs, cols)
mutate_(.tbl, .dots = vars)
}
#' @rdname summarise_all
#' @export
summarize_all <- summarise_all
#' @rdname summarise_all
#' @export
summarize_at <- summarise_at
#' @rdname summarise_all
#' @export
summarize_if <- summarise_if
#' Select columns
#'
#' This helper has equivalent semantics to \code{\link{select}()}. Its
#' purpose is to provide \code{select()} semantics to the colwise
#' summarising and mutating verbs.
#' @param ... Variables to include/exclude in mutate/summarise. You
#' can use same specifications as in \code{\link{select}}. If
#' missing, defaults to all non-grouping variables.
#' @seealso \code{\link{summarise_all}()}
#' @export
vars <- function(...) {
structure(lazyeval::lazy_dots(...),
class = c("col_list", "lazy_dots")
)
}
is_col_list <- function(cols) inherits(cols, "col_list")
select_colwise_names <- function(tbl, cols) {
vars <- tbl_vars(tbl)
if (is.character(cols) || is_col_list(cols)) {
selected <- cols
} else if (is.numeric(cols)) {
selected <- vars[cols]
} else {
stop(".cols should be a character/numeric vector or a columns object",
call. = FALSE)
}
selected
}
colwise_ <- function(tbl, calls, vars) {
stopifnot(is.fun_list(calls))
named_calls <- attr(calls, "has_names")
named_vars <- any(has_names(vars))
vars <- select_vars_(tbl_vars(tbl), vars, exclude = as.character(groups(tbl)))
out <- vector("list", length(vars) * length(calls))
dim(out) <- c(length(vars), length(calls))
vars <- enc2native(vars)
for (i in seq_along(vars)) {
for (j in seq_along(calls)) {
out[[i, j]] <- lazyeval::interp(calls[[j]],
.values = list(. = as.name(vars[i])))
}
}
dim(out) <- NULL
if (length(calls) == 1 && !named_calls) {
names(out) <- names(vars)
} else if (length(vars) == 1 && !named_vars) {
names(out) <- names(calls)
} else {
grid <- expand.grid(var = names(vars), call = names(calls))
names(out) <- paste(grid$var, grid$call, sep = "_")
}
out
}
#' Summarise and mutate multiple columns.
#'
#' Apply one or more functions to one or more columns. Grouping variables
#' are always excluded from modification.
#'
#' In the future \code{mutate_each()} and \code{summarise_each()} will
#' be deprecated in favour of a more featureful family of functions:
#' \code{\link{mutate_all}()}, \code{\link{mutate_at}()},
#' \code{\link{mutate_if}()}, \code{\link{summarise_all}()},
#' \code{\link{summarise_at}()} and \code{\link{summarise_if}()}.
#' @param tbl a tbl
#' @param funs List of function calls, generated by \code{\link{funs}}, or
#' a character vector of function names.
#' @param vars,... Variables to include/exclude in mutate/summarise.
#' You can use same specifications as in \code{\link{select}}. If missing,
#' defaults to all non-grouping variables.
#'
#' For standard evaluation versions (ending in \code{_}) these can
#' be either a list of expressions or a character vector.
#' @export
summarise_each <- function(tbl, funs, ...) {
summarise_each_(tbl, funs, lazyeval::lazy_dots(...))
}
#' @export
#' @rdname summarise_each
summarise_each_ <- function(tbl, funs, vars) {
if (length(vars) == 0) {
vars <- lazyeval::lazy_dots(everything())
}
if (is.character(funs)) {
funs <- funs_(funs)
}
vars <- colwise_(tbl, funs, vars)
summarise_(tbl, .dots = vars)
}
#' @rdname summarise_each
#' @export
summarize_each <- summarise_each
#' @rdname summarise_each
#' @export
summarize_each_ <- summarise_each_
#' @export
#' @rdname summarise_each
mutate_each <- function(tbl, funs, ...) {
if (is.character(funs)) {
funs <- funs_(funs)
}
mutate_each_(tbl, funs, lazyeval::lazy_dots(...))
}
#' @export
#' @rdname summarise_each
mutate_each_ <- function(tbl, funs, vars) {
if (length(vars) == 0) {
vars <- lazyeval::lazy_dots(everything())
}
vars <- colwise_(tbl, funs, vars)
mutate_(tbl, .dots = vars)
}
#' @export
summarise_each_q <- function(...) {
.Deprecated("summarise_all")
summarise_each_(...)
}
#' @export
mutate_each_q <- function(...) {
.Deprecated("mutate_all")
mutate_each_(...)
}