@@ -292,12 +292,26 @@ rbind_dfs <- function(dfs) {
292
292
allocated [new_columns ] <- TRUE
293
293
if (all(allocated )) break
294
294
}
295
+ is_date <- lapply(out , inherits , ' Date' )
296
+ is_time <- lapply(out , inherits , ' POSIXct' )
295
297
pos <- c(cumsum(nrows ) - nrows + 1 )
296
298
for (i in seq_along(dfs )) {
297
299
df <- dfs [[i ]]
298
300
rng <- seq(pos [i ], length.out = nrows [i ])
299
301
for (col in names(df )) {
300
- if (inherits(df [[col ]], ' factor' )) {
302
+ date_col <- inherits(df [[col ]], ' Date' )
303
+ time_col <- inherits(df [[col ]], ' POSIXct' )
304
+ if (is_date [[col ]] && ! date_col ) {
305
+ out [[col ]][rng ] <- as.Date(
306
+ unclass(df [[col ]]),
307
+ origin = ggplot_global $ date_origin
308
+ )
309
+ } else if (is_time [[col ]] && ! time_col ) {
310
+ out [[col ]][rng ] <- as.POSIXct(
311
+ unclass(df [[col ]]),
312
+ origin = ggplot_global $ time_origin
313
+ )
314
+ } else if (date_col || time_col || inherits(df [[col ]], ' factor' )) {
301
315
out [[col ]][rng ] <- as.character(df [[col ]])
302
316
} else {
303
317
out [[col ]][rng ] <- df [[col ]]
@@ -307,7 +321,11 @@ rbind_dfs <- function(dfs) {
307
321
for (col in names(col_levels )) {
308
322
out [[col ]] <- factor (out [[col ]], levels = col_levels [[col ]])
309
323
}
310
- attributes(out ) <- list (class = " data.frame" , names = names(out ), row.names = .set_row_names(total ))
324
+ attributes(out ) <- list (
325
+ class = " data.frame" ,
326
+ names = names(out ),
327
+ row.names = .set_row_names(total )
328
+ )
311
329
out
312
330
}
313
331
# ' Apply function to unique subsets of a data.frame
@@ -333,6 +351,7 @@ dapply <- function(df, by, fun, ..., drop = TRUE) {
333
351
grouping_cols <- .subset(df , by )
334
352
ids <- id(grouping_cols , drop = drop )
335
353
group_rows <- split(seq_len(nrow(df )), ids )
354
+ fallback_order <- unique(c(by , names(df )))
336
355
rbind_dfs(lapply(seq_along(group_rows ), function (i ) {
337
356
cur_data <- df_rows(df , group_rows [[i ]])
338
357
res <- fun(cur_data , ... )
@@ -341,6 +360,8 @@ dapply <- function(df, by, fun, ..., drop = TRUE) {
341
360
vars <- lapply(setNames(by , by ), function (col ) .subset2(cur_data , col )[1 ])
342
361
if (is.matrix(res )) res <- split_matrix(res )
343
362
if (is.null(names(res ))) names(res ) <- paste0(" V" , seq_along(res ))
344
- new_data_frame(modify_list(unclass(vars ), unclass(res )))
363
+ if (all(by %in% names(res ))) return (new_data_frame(unclass(res )))
364
+ res <- modify_list(unclass(vars ), unclass(res ))
365
+ new_data_frame(res [intersect(c(fallback_order , names(res )), names(res ))])
345
366
}))
346
367
}
0 commit comments