Skip to content

Commit 8d99c96

Browse files
authored
Merge pull request #90 from B0ydT/group_split
Group split
2 parents f249098 + 0ed9b5f commit 8d99c96

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ S3method(filter,SummarizedExperiment)
1111
S3method(full_join,SummarizedExperiment)
1212
S3method(ggplot,SummarizedExperiment)
1313
S3method(group_by,SummarizedExperiment)
14+
S3method(group_split,SummarizedExperiment)
1415
S3method(inner_join,SummarizedExperiment)
1516
S3method(left_join,SummarizedExperiment)
1617
S3method(mutate,SummarizedExperiment)
@@ -64,6 +65,7 @@ importFrom(dplyr,filter)
6465
importFrom(dplyr,full_join)
6566
importFrom(dplyr,group_by)
6667
importFrom(dplyr,group_by_drop_default)
68+
importFrom(dplyr,group_split)
6769
importFrom(dplyr,inner_join)
6870
importFrom(dplyr,left_join)
6971
importFrom(dplyr,mutate)

R/dplyr_methods.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,3 +862,46 @@ pull.SummarizedExperiment <- function(.data, var=-1, name=NULL, ...) {
862862
as_tibble(skip_GRanges=skip_GRanges) |>
863863
dplyr::pull(var=!!var, name=!!name, ...)
864864
}
865+
866+
#' @name group_split
867+
#' @rdname group_split
868+
#' @inherit dplyr::group_split
869+
#'
870+
#' @examples
871+
#' data(pasilla, package = "tidySummarizedExperiment")
872+
#' pasilla |> group_split(condition)
873+
#' pasilla |> group_split(counts > 0)
874+
#' pasilla |> group_split(condition, counts > 0)
875+
#'
876+
#' @importFrom ellipsis check_dots_used
877+
#' @importFrom dplyr group_split
878+
#' @export
879+
group_split.SummarizedExperiment <- function(.tbl, ..., .keep = TRUE) {
880+
881+
var_list <- enquos(...)
882+
data_nested <- NULL
883+
884+
nested <- .tbl |>
885+
mutate(!!!var_list) |>
886+
nest(data_nested = -(substring(as.character(var_list), 2)))
887+
888+
if(.keep) {
889+
grouped_data <- nested |>
890+
pull(data_nested)
891+
892+
grouping_cols <- nested |>
893+
select(substring(as.character(var_list), 2))
894+
895+
for(i in 1:length(grouped_data)) {
896+
grouped_data[[i]] <- grouped_data[[i]] |>
897+
mutate(grouping_cols[i,])
898+
}
899+
900+
grouped_data
901+
902+
} else {
903+
nested |>
904+
pull(data_nested)
905+
}
906+
907+
}

man/group_split.Rd

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-dplyr_methods.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,27 @@ test_that("mutate counts", {
181181
expect_equal(TRUE)
182182

183183
})
184+
185+
test_that("group_split splits character columns", {
186+
data(pasilla)
187+
pasilla |>
188+
group_split(condition) |>
189+
length() |>
190+
expect_equal(2)
191+
})
192+
193+
test_that("group_split splits logical comparisons", {
194+
data(pasilla)
195+
pasilla |>
196+
group_split(counts > 0) |>
197+
length() |>
198+
expect_equal(2)
199+
})
200+
201+
test_that("group_split splits with mutliple arguments", {
202+
data(pasilla)
203+
pasilla |>
204+
group_split(condition, counts > 0) |>
205+
length() |>
206+
expect_equal(4)
207+
})

0 commit comments

Comments
 (0)