Skip to content

Commit

Permalink
fix: Ensure single column dfs work with group_split()
Browse files Browse the repository at this point in the history
Fixes #124
  • Loading branch information
nathaneastwood committed Feb 8, 2024
1 parent a7ff161 commit a531324
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Suggests:
tinytest
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Language: en-GB
6 changes: 3 additions & 3 deletions R/group_split.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#' @details
#' **Grouped `data.frame`s:**
#'
#' The primary use case for `group_split()` is with already groups `data.frame`s, typically a result of [group_by()]. In
#' this case, `group_split()` only uses the first argument, the grouped `data.frame`, and warns when `...` is used.
#' The primary use case for `group_split()` is with already grouped `data.frame`s, typically a result of [group_by()].
#' In this case, `group_split()` only uses the first argument, the grouped `data.frame`, and warns when `...` is used.
#'
#' Because some of these groups may be empty, it is best paired with `group_keys()` which identifies the representatives
#' of each grouping variable for the group.
Expand Down Expand Up @@ -60,7 +60,7 @@ group_split <- function(.data, ..., .keep = TRUE) {
res <- split_into_groups(context$.data, groups)
names(res) <- NULL
if (!isTRUE(.keep)) {
res <- lapply(res, function(x) x[, !colnames(x) %in% groups])
res <- lapply(res, function(x) x[, !colnames(x) %in% groups, drop = FALSE])
}
any_empty <- unlist(lapply(res, function(x) !(nrow(x) == 0L)))
res[any_empty]
Expand Down
17 changes: 14 additions & 3 deletions R/nest_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ nest_by.grouped_df <- function(.data, ..., .key = "data", .keep = FALSE) {
if (!missing(...)) {
stop("Can't re-group while nesting. Either `ungroup()` first or don't supply arguments to `nest_by()`")
}
res <- group_keys(.data)
res[[.key]] <- group_split(.data, ..., .keep = .keep)
do.call(group_by, list(res, as.symbol(group_vars(.data))))
nests <- group_split(.data, ..., .keep = TRUE)
groups <- group_vars(.data)
res <- lapply(
nests,
function(x) {
df <- x[1, groups, drop = FALSE]
df[[.key]] <- list(x[, !colnames(x) %in% groups, drop = FALSE])
df
}
)
res <- do.call(rbind, res)
rownames(res) <- NULL
res <- do.call(arrange, list(res, as.symbol(groups)))
do.call(group_by, list(res, as.symbol(groups)))
}
20 changes: 20 additions & 0 deletions inst/tinytest/test_nest_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ expect_equal(
info = "nest_by() takes group names"
)

expect_equal(
mtcars |> select(mpg, cyl, am) |> nest_by(am, cyl),
{
res <- data.frame(
am = c(0, 0, 0, 1, 1, 1),
cyl = c(4, 6, 8, 4, 6, 8)
)
res[["data"]] <- list(
mtcars[mtcars$am == 0 & mtcars$cyl == 4, "mpg", drop = FALSE],
mtcars[mtcars$am == 0 & mtcars$cyl == 6, "mpg", drop = FALSE],
mtcars[mtcars$am == 0 & mtcars$cyl == 8, "mpg", drop = FALSE],
mtcars[mtcars$am == 1 & mtcars$cyl == 4, "mpg", drop = FALSE],
mtcars[mtcars$am == 1 & mtcars$cyl == 6, "mpg", drop = FALSE],
mtcars[mtcars$am == 1 & mtcars$cyl == 8, "mpg", drop = FALSE]
)
group_by(res, am)
},
info = "nest_by() works with only one non-group column (#124)"
)

expect_equal(
mtcars %>% group_by(am) %>% nest_by(),
{
Expand Down
4 changes: 2 additions & 2 deletions man/group_split.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a531324

Please sign in to comment.