forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbl-df.r
229 lines (197 loc) · 6.27 KB
/
tbl-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
#' Create a data frame tbl.
#'
#' Forwards the argument to \code{\link[tibble]{as_data_frame}}, see
#' \link{tibble-package} for more details.
#'
#' @export
#' @param data a data frame
#' @examples
#' ds <- tbl_df(mtcars)
#' ds
#' as.data.frame(ds)
#'
#' if (require("Lahman") && packageVersion("Lahman") >= "3.0.1") {
#' batting <- tbl_df(Batting)
#' dim(batting)
#' colnames(batting)
#' head(batting)
#'
#' # Data manipulation verbs ---------------------------------------------------
#' filter(batting, yearID > 2005, G > 130)
#' select(batting, playerID:lgID)
#' arrange(batting, playerID, desc(yearID))
#' summarise(batting, G = mean(G), n = n())
#' mutate(batting, rbi2 = if(is.null(AB)) 1.0 * R / AB else 0)
#'
#' # Group by operations -------------------------------------------------------
#' # To perform operations by group, create a grouped object with group_by
#' players <- group_by(batting, playerID)
#' head(group_size(players), 100)
#'
#' summarise(players, mean_g = mean(G), best_ab = max(AB))
#' best_year <- filter(players, AB == max(AB) | G == max(G))
#' progress <- mutate(players, cyear = yearID - min(yearID) + 1,
#' rank(desc(AB)), cumsum(AB))
#'
#' # When you group by multiple level, each summarise peels off one level
#' \donttest{
#' per_year <- group_by(batting, playerID, yearID)
#' stints <- summarise(per_year, stints = max(stint))
#' filter(stints, stints > 3)
#' summarise(stints, max(stints))
#' mutate(stints, cumsum(stints))
#' }
#'
#' # Joins ---------------------------------------------------------------------
#' player_info <- select(tbl_df(Master), playerID, birthYear)
#' hof <- select(filter(tbl_df(HallOfFame), inducted == "Y"),
#' playerID, votedBy, category)
#'
#' # Match players and their hall of fame data
#' inner_join(player_info, hof)
#' # Keep all players, match hof data where available
#' left_join(player_info, hof)
#' # Find only players in hof
#' semi_join(player_info, hof)
#' # Find players not in hof
#' anti_join(player_info, hof)
#' }
tbl_df <- function(data) {
as_data_frame(data)
}
#' @export
as.tbl.data.frame <- function(x, ...) {
tbl_df(x)
}
#' @export
tbl_vars.data.frame <- function(x) names(x)
#' @export
same_src.data.frame <- function(x, y) {
is.data.frame(y)
}
#' @export
auto_copy.tbl_df <- function(x, y, copy = FALSE, ...) {
as.data.frame(y)
}
# Grouping methods ------------------------------------------------------------
# These are all inherited from data.frame - see tbl-data-frame.R
# Standard data frame methods --------------------------------------------------
#' @export
as.data.frame.tbl_df <- function(x, row.names = NULL, optional = FALSE, ...) {
as_regular_df(x)
}
# Verbs ------------------------------------------------------------------------
#' @export
arrange_.tbl_df <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
arrange_impl(.data, dots)
}
#' @export
filter_.tbl_df <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ...)
if (any(has_names(dots))) {
stop("filter() takes unnamed arguments. Do you need `==`?", call. = FALSE)
}
# C++ code assumes that elements are named, so give them automatic names
dots <- lazyeval::auto_name(dots)
filter_impl(.data, dots)
}
#' @export
slice_.tbl_df <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
slice_impl(.data, dots)
}
#' @export
mutate_.tbl_df <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
mutate_impl(.data, dots)
}
#' @export
summarise_.tbl_df <- function(.data, ..., .dots) {
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE)
summarise_impl(.data, dots)
}
# Joins ------------------------------------------------------------------------
#' Join data frame tbls.
#'
#' See \code{\link{join}} for a description of the general purpose of the
#' functions.
#'
#' @inheritParams inner_join
#' @param ... included for compatibility with the generic; otherwise ignored.
#' @examples
#' if (require("Lahman")) {
#' batting_df <- tbl_df(Batting)
#' person_df <- tbl_df(Master)
#'
#' uperson_df <- tbl_df(Master[!duplicated(Master$playerID), ])
#'
#' # Inner join: match batting and person data
#' inner_join(batting_df, person_df)
#' inner_join(batting_df, uperson_df)
#'
#' # Left join: match, but preserve batting data
#' left_join(batting_df, uperson_df)
#'
#' # Anti join: find batters without person data
#' anti_join(batting_df, person_df)
#' # or people who didn't bat
#' anti_join(person_df, batting_df)
#' }
#' @name join.tbl_df
NULL
#' @export
#' @rdname join.tbl_df
inner_join.tbl_df <- function(x, y, by = NULL, copy = FALSE,
suffix = c(".x", ".y"), ...) {
by <- common_by(by, x, y)
suffix <- check_suffix(suffix)
y <- auto_copy(x, y, copy = copy)
inner_join_impl(x, y, by$x, by$y, suffix$x, suffix$y)
}
#' @export
#' @rdname join.tbl_df
left_join.tbl_df <- function(x, y, by = NULL, copy = FALSE,
suffix = c(".x", ".y"), ...) {
by <- common_by(by, x, y)
suffix <- check_suffix(suffix)
y <- auto_copy(x, y, copy = copy)
left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y)
}
#' @export
#' @rdname join.tbl_df
right_join.tbl_df <- function(x, y, by = NULL, copy = FALSE,
suffix = c(".x", ".y"), ...) {
by <- common_by(by, x, y)
suffix <- check_suffix(suffix)
y <- auto_copy(x, y, copy = copy)
right_join_impl(x, y, by$x, by$y, suffix$x, suffix$y)
}
#' @export
#' @rdname join.tbl_df
full_join.tbl_df <- function(x, y, by = NULL, copy = FALSE,
suffix = c(".x", ".y"), ...) {
by <- common_by(by, x, y)
suffix <- check_suffix(suffix)
y <- auto_copy(x, y, copy = copy)
full_join_impl(x, y, by$x, by$y, suffix$x, suffix$y)
}
#' @export
#' @rdname join.tbl_df
semi_join.tbl_df <- function(x, y, by = NULL, copy = FALSE, ...) {
by <- common_by(by, x, y)
y <- auto_copy(x, y, copy = copy)
semi_join_impl(x, y, by$x, by$y)
}
#' @export
#' @rdname join.tbl_df
anti_join.tbl_df <- function(x, y, by = NULL, copy = FALSE, ...) {
by <- common_by(by, x, y)
y <- auto_copy(x, y, copy = copy)
anti_join_impl(x, y, by$x, by$y)
}
# Set operations ---------------------------------------------------------------
#' @export
distinct_.tbl_df <- function(.data, ..., .dots) {
tbl_df(NextMethod())
}