forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataframe.R
241 lines (189 loc) · 6.58 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
methods::setOldClass(c("grouped_df", "tbl_df", "data.frame"))
# Grouping methods ------------------------------------------------------------
#' Convert to a data frame
#'
#' Functions that convert the input to a \code{data_frame}.
#'
#' @details For a grouped data frame, the \code{\link[tibble]{as_data_frame}}
#' S3 generic simply removes the grouping.
#'
#' @inheritParams tibble::as_data_frame
#' @seealso \code{\link[tibble]{as_data_frame}}
#' @name grouped_df
#' @export
as_data_frame.grouped_df <- function(x, ...) {
x <- ungroup(x)
class(x) <- c("tbl_df", "tbl", "data.frame")
x
}
#' Convert row names to an explicit variable.
#'
#' Deprecated, use \code{\link[tibble]{rownames_to_column}} instead.
#'
#' @param df Input data frame with rownames.
#' @param var Name of variable to use
#' @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, ..., .dots, add = FALSE) {
groups <- group_by_prepare(.data, ..., .dots = .dots, add = add)
grouped_df(groups$data, groups$groups)
}
#' @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, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
as.data.frame(filter_(tbl_df(.data), .dots = dots))
}
#' @export
slice_.data.frame <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
as.data.frame(slice_(tbl_df(.data), .dots = dots))
}
#' @export
summarise_.data.frame <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
as.data.frame(summarise_(tbl_df(.data), .dots = dots))
}
#' @export
mutate_.data.frame <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
as.data.frame(mutate_(tbl_df(.data), .dots = dots))
}
#' @export
arrange_.data.frame <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
as.data.frame(arrange_(tbl_df(.data), .dots = dots))
}
#' @export
select_.data.frame <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
vars <- select_vars_(names(.data), dots)
select_impl(.data, vars)
}
#' @export
rename_.data.frame <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
vars <- rename_vars_(names(.data), dots)
select_impl(.data, vars)
}
# 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, ..., .dots, .keep_all = FALSE) {
dist <- distinct_vars(.data, ..., .dots = .dots, .keep_all = .keep_all)
distinct_impl(dist$data, dist$vars, dist$keep)
}
# Do ---------------------------------------------------------------------------
#' @export
do_.data.frame <- function(.data, ..., .dots) {
args <- lazyeval::all_dots(.dots, ...)
named <- named_args(args)
data <- list(. = .data)
if (!named) {
env <- new.env(parent = args[[1]]$env)
env$. <- .data
out <- lazyeval::lazy_eval(args[[1]], data)
if (!is.data.frame(out)) {
stop("Result must be a data frame", call. = FALSE)
}
} else {
out <- lapply(args, function(arg) {
list(lazyeval::lazy_eval(arg, data))
})
names(out) <- names(args)
attr(out, "row.names") <- .set_row_names(1L)
# Use tbl_df to ensure safe printing of list columns
class(out) <- c("tbl_df", "data.frame")
}
out
}
# 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