forked from tidyverse/dplyr
/
dataframe.R
265 lines (209 loc) · 7.08 KB
/
dataframe.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
methods::setOldClass(c("grouped_df", "tbl_df", "data.frame"))
# Grouping methods ------------------------------------------------------------
#' Convert row names to an explicit variable.
#'
#' Deprecated, use [tibble::rownames_to_column()] instead.
#'
#' @param df Input data frame with rownames.
#' @param var Name of variable to use
#' @keywords internal
#' @export
#' @examples
#' mtcars %>% tbl_df()
#'
#' mtcars %>% add_rownames()
add_rownames <- function(df, var = "rowname") {
warning(
"Deprecated, use tibble::rownames_to_column() instead.",
call. = FALSE)
stopifnot(is.data.frame(df))
rn <- as_data_frame(setNames(list(rownames(df)), var))
rownames(df) <- NULL
bind_cols(rn, df)
}
# Grouping methods ------------------------------------------------------------
#' @export
group_by.data.frame <- function(.data, ..., add = FALSE) {
groups <- group_by_prepare(.data, ..., add = add)
grouped_df(groups$data, groups$group_names)
}
#' @export
group_by_.data.frame <- function(.data, ..., .dots = list(), add = FALSE) {
dots <- compat_lazy_dots(.dots, caller_env(), ...)
group_by(.data, !!! dots, add = add)
}
#' @export
groups.data.frame <- function(x) NULL
#' @export
ungroup.data.frame <- function(x, ...) x
#' @export
group_size.data.frame <- function(x) nrow(x)
#' @export
n_groups.data.frame <- function(x) 1L
# Manipulation functions ------------------------------------------------------
# These could potentially be rewritten to avoid any copies, but since this
# is just a convenience layer, I didn't bother. They should still be fast.
#' @export
filter.data.frame <- function(.data, ...) {
as.data.frame(filter(tbl_df(.data), ...))
}
#' @export
filter_.data.frame <- function(.data, ..., .dots = list()) {
as.data.frame(filter_(tbl_df(.data), ..., .dots = .dots))
}
#' @export
slice.data.frame <- function(.data, ...) {
dots <- dots_quosures(..., .named = TRUE)
slice_impl(.data, dots)
}
#' @export
slice_.data.frame <- function(.data, ..., .dots = list()) {
dots <- compat_lazy_dots(.dots, caller_env(), ...)
slice_impl(.data, dots)
}
#' @export
summarise.data.frame <- function(.data, ...) {
as.data.frame(summarise(tbl_df(.data), ...))
}
#' @export
summarise_.data.frame <- function(.data, ..., .dots = list()) {
as.data.frame(summarise_(tbl_df(.data), ..., .dots = .dots))
}
#' @export
mutate.data.frame <- function(.data, ...) {
as.data.frame(mutate(tbl_df(.data), ...))
}
#' @export
mutate_.data.frame <- function(.data, ..., .dots = list()) {
as.data.frame(mutate_(tbl_df(.data), ..., .dots = .dots))
}
#' @export
arrange.data.frame <- function(.data, ...) {
as.data.frame(arrange(tbl_df(.data), ...))
}
#' @export
arrange_.data.frame <- function(.data, ..., .dots = list()) {
as.data.frame(arrange_(tbl_df(.data), ...), .dots = .dots)
}
#' @export
select.data.frame <- function(.data, ...) {
vars <- select_vars(names(.data), ...)
select_impl(.data, vars)
}
#' @export
select_.data.frame <- function(.data, ..., .dots = list()) {
dots <- compat_lazy_dots(.dots, caller_env(), ...)
select(.data, !!! dots)
}
#' @export
rename.data.frame <- function(.data, ...) {
vars <- rename_vars(names(.data), ...)
select_impl(.data, vars)
}
#' @export
rename_.data.frame <- function(.data, ..., .dots = list()) {
dots <- compat_lazy_dots(.dots, caller_env(), ...)
rename(.data, !!! dots)
}
# Joins ------------------------------------------------------------------------
#' @export
inner_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
as.data.frame(inner_join(tbl_df(x), y, by = by, copy = copy, ...))
}
#' @export
left_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
as.data.frame(left_join(tbl_df(x), y, by = by, copy = copy, ...))
}
#' @export
right_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
as.data.frame(right_join(tbl_df(x), y, by = by, copy = copy, ...))
}
#' @export
full_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
as.data.frame(full_join(tbl_df(x), y, by = by, copy = copy, ...))
}
#' @export
semi_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
as.data.frame(semi_join(tbl_df(x), y, by = by, copy = copy, ...))
}
#' @export
anti_join.data.frame <- function(x, y, by = NULL, copy = FALSE, ...) {
as.data.frame(anti_join(tbl_df(x), y, by = by, copy = copy, ...))
}
# Set operations ---------------------------------------------------------------
#' @export
intersect.data.frame <- function(x, y, ...) intersect_data_frame(x, y)
#' @export
union.data.frame <- function(x, y, ...) union_data_frame(x, y)
#' @export
union_all.data.frame <- function(x, y, ...) bind_rows(x, y)
#' @export
setdiff.data.frame <- function(x, y, ...) setdiff_data_frame(x, y)
#' @export
setequal.data.frame <- function(x, y, ...) equal_data_frame(x, y)
#' @export
distinct.data.frame <- function(.data, ..., .keep_all = FALSE) {
dist <- distinct_vars(.data, ..., .keep_all = .keep_all)
distinct_impl(dist$data, dist$vars, dist$keep)
}
#' @export
distinct_.data.frame <- function(.data, ..., .dots = list(), .keep_all = FALSE) {
dots <- compat_lazy_dots(.dots, caller_env(), ...)
distinct(.data, !!! dots, .keep_all = .keep_all)
}
# Do ---------------------------------------------------------------------------
#' @export
do.data.frame <- function(.data, ...) {
args <- dots_quosures(...)
named <- named_args(args)
# Create custom dynamic scope with `.` pronoun
overscope <- child_env(data = list(. = .data, .data = .data))
if (!named) {
out <- eval_tidy_(args[[1]], overscope)
if (!inherits(out, "data.frame")) {
abort("Result must be a data frame")
}
} else {
out <- map(args, function(arg) list(eval_tidy_(arg, overscope)))
names(out) <- names(args)
out <- tibble::as_tibble(out, validate = FALSE)
}
out
}
#' @export
do_.data.frame <- function(.data, ..., .dots = list()) {
dots <- compat_lazy_dots(.dots, caller_env(), ...)
do(.data, !!! dots)
}
# Random samples ---------------------------------------------------------------
#' @export
sample_n.data.frame <- function(tbl, size, replace = FALSE, weight = NULL,
.env = parent.frame()) {
if (!missing(weight)) {
weight <- eval(substitute(weight), tbl, .env)
}
sample_n_basic(tbl, size, replace = replace, weight = weight)
}
#' @export
sample_frac.data.frame <- function(tbl, size = 1, replace = FALSE, weight = NULL,
.env = parent.frame()) {
if (!missing(weight)) {
weight <- eval(substitute(weight), tbl, .env)
}
sample_n_basic(tbl, round(size * nrow(tbl)), replace = replace, weight = weight)
}
sample_n_basic <- function(tbl, size, replace = FALSE, weight = NULL) {
n <- nrow(tbl)
weight <- check_weight(weight, n)
assert_that(is.numeric(size), length(size) == 1, size >= 0)
check_size(size, n, replace)
idx <- sample.int(n, size, replace = replace, prob = weight)
tbl[idx, , drop = FALSE]
}
# Misc -------------------------------------------------------------------------
#' @export
collect.data.frame <- function(x, ...) x
#' @export
compute.data.frame <- function(x, ...) x
#' @export
collapse.data.frame <- function(x, ...) x