group_split() works like base::split() but:
It uses the grouping structure from group_by() and therefore is subject
to the data mask
It does not name the elements of the list based on the grouping as this
only works well for a single character grouping variable. Instead,
use group_keys() to access a data frame that defines the groups.
group_split() is primarily designed to work with grouped data frames.
You can pass ... to group and split an ungrouped data frame, but this
is generally not very useful as you want have easy access to the group
metadata.
# S3 method for class 'SummarizedExperiment'
group_split(.tbl, ..., .keep = TRUE)A tbl.
If .tbl is an ungrouped data frame, a grouping specification,
forwarded to group_by().
Should the grouping columns be kept?
A list of tibbles. Each tibble contains the rows of .tbl for the
associated group and all the columns, including the grouping variables.
Note that this returns a list_of which is slightly
stricter than a simple list but is useful for representing lists where
every element has the same type.
group_split() is not stable because you can achieve very similar results by
manipulating the nested column returned from
tidyr::nest(.by =). That also retains the group keys all
within a single data structure. group_split() may be deprecated in the
future.
Hutchison, W.J., Keyes, T.J., The tidyomics Consortium. et al. The tidyomics ecosystem: enhancing omic data analyses. Nat Methods 21, 1166–1170 (2024). https://doi.org/10.1038/s41592-024-02299-2
Wickham, H., François, R., Henry, L., Müller, K., Vaughan, D. (2023). dplyr: A Grammar of Data Manipulation. R package version 2.1.4, https://CRAN.R-project.org/package=dplyr
Other grouping functions:
group_by(),
group_map(),
group_nest(),
group_trim()
data(pasilla, package = "tidySummarizedExperiment")
pasilla |> group_split(condition)
#> [[1]]
#> # A SummarizedExperiment-tibble abstraction: Features=14599 | Samples=4 |
#> # Assays=counts
#> # |------ COVARIATES ----|
#> .feature .sample | counts | type condition |
#> <chr> <chr> | <chr> | <chr> <chr> |
#> 1 FBgn0000003 untrt1 | 0 | single_end untreated |
#> 2 FBgn0000008 untrt1 | 92 | single_end untreated |
#> 3 FBgn0000014 untrt1 | 5 | single_end untreated |
#> 4 FBgn0000015 untrt1 | 0 | single_end untreated |
#> 5 FBgn0000017 untrt1 | 4664 | single_end untreated |
#> -------- ------- - ------ - ---- --------- -
#> 58392 FBgn0261571 untrt4 | 0 | paired_end untreated |
#> 58393 FBgn0261572 untrt4 | 11 | paired_end untreated |
#> 58394 FBgn0261573 untrt4 | 1612 | paired_end untreated |
#> 58395 FBgn0261574 untrt4 | 2819 | paired_end untreated |
#> 58396 FBgn0261575 untrt4 | 3 | paired_end untreated |
#>
#> [[2]]
#> # A SummarizedExperiment-tibble abstraction: Features=14599 | Samples=3 |
#> # Assays=counts
#> # |------ COVARIATES ----|
#> .feature .sample | counts | type condition |
#> <chr> <chr> | <chr> | <chr> <chr> |
#> 1 FBgn0000003 trt1 | 0 | single_end treated |
#> 2 FBgn0000008 trt1 | 140 | single_end treated |
#> 3 FBgn0000014 trt1 | 4 | single_end treated |
#> 4 FBgn0000015 trt1 | 1 | single_end treated |
#> 5 FBgn0000017 trt1 | 6205 | single_end treated |
#> -------- ------- - ------ - ---- --------- -
#> 43793 FBgn0261571 trt3 | 0 | paired_end treated |
#> 43794 FBgn0261572 trt3 | 3 | paired_end treated |
#> 43795 FBgn0261573 trt3 | 1908 | paired_end treated |
#> 43796 FBgn0261574 trt3 | 3047 | paired_end treated |
#> 43797 FBgn0261575 trt3 | 4 | paired_end treated |
#>
pasilla |> group_split(counts > 0)
#> ! tidySummarizedExperiment says: there are NON-numerical columns, the matrix
#> will NOT be numerical
#> [[1]]
#> # A SummarizedExperiment-tibble abstraction: Features=5536 | Samples=7 |
#> # Assays=counts
#> # |------------ COVARIATES -----------|
#> .feature .sample | counts | condition type `counts > 0` |
#> <chr> <chr> | <chr> | <chr> <chr> <chr> |
#> 1 FBgn0000003 untrt1 | 0 | untreated single_end FALSE |
#> 2 FBgn0000015 untrt1 | 0 | untreated single_end FALSE |
#> 3 FBgn0000022 untrt1 | 0 | untreated single_end FALSE |
#> 4 FBgn0000028 untrt1 | 0 | untreated single_end FALSE |
#> 5 FBgn0000038 untrt1 | 0 | untreated single_end FALSE |
#> -------- ------- - ------ - --------- ---- ---------- -
#> 38748 FBgn0086665 trt3 | 0 | treated paired_end FALSE |
#> 38749 FBgn0259737 trt3 | 0 | treated paired_end FALSE |
#> 38750 FBgn0260000 trt3 | 0 | treated paired_end FALSE |
#> 38751 FBgn0261508 trt3 | 0 | treated paired_end FALSE |
#> 38752 FBgn0261514 trt3 | 0 | treated paired_end FALSE |
#>
#> [[2]]
#> # A SummarizedExperiment-tibble abstraction: Features=12359 | Samples=7 |
#> # Assays=counts
#> # |------------ COVARIATES -----------|
#> .feature .sample | counts | condition type `counts > 0` |
#> <chr> <chr> | <chr> | <chr> <chr> <chr> |
#> 1 FBgn0000008 untrt1 | 92 | untreated single_end TRUE |
#> 2 FBgn0000014 untrt1 | 5 | untreated single_end TRUE |
#> 3 FBgn0000017 untrt1 | 4664 | untreated single_end TRUE |
#> 4 FBgn0000018 untrt1 | 583 | untreated single_end TRUE |
#> 5 FBgn0000024 untrt1 | 10 | untreated single_end TRUE |
#> -------- ------- - ------ - --------- ---- ---------- -
#> 86509 FBgn0085319 trt3 | 1 | treated paired_end TRUE |
#> 86510 FBgn0259196 trt3 | 2 | treated paired_end TRUE |
#> 86511 FBgn0259998 trt3 | 1 | treated paired_end TRUE |
#> 86512 FBgn0261401 trt3 | 1 | treated paired_end TRUE |
#> 86513 FBgn0261568 trt3 | 1 | treated paired_end TRUE |
#>
pasilla |> group_split(condition, counts > 0)
#> ! tidySummarizedExperiment says: there are NON-numerical columns, the matrix
#> will NOT be numerical
#> [[1]]
#> # A SummarizedExperiment-tibble abstraction: Features=5271 | Samples=4 |
#> # Assays=counts
#> # |------------ COVARIATES -----------|
#> .feature .sample | counts | type condition `counts > 0` |
#> <chr> <chr> | <chr> | <chr> <chr> <chr> |
#> 1 FBgn0000003 untrt1 | 0 | single_end untreated FALSE |
#> 2 FBgn0000015 untrt1 | 0 | single_end untreated FALSE |
#> 3 FBgn0000022 untrt1 | 0 | single_end untreated FALSE |
#> 4 FBgn0000028 untrt1 | 0 | single_end untreated FALSE |
#> 5 FBgn0000038 untrt1 | 0 | single_end untreated FALSE |
#> -------- ------- - ------ - ---- --------- ---------- -
#> 21080 FBgn0260718 untrt4 | 0 | paired_end untreated FALSE |
#> 21081 FBgn0260964 untrt4 | 0 | paired_end untreated FALSE |
#> 21082 FBgn0260967 untrt4 | 0 | paired_end untreated FALSE |
#> 21083 FBgn0260968 untrt4 | 0 | paired_end untreated FALSE |
#> 21084 FBgn0261356 untrt4 | 0 | paired_end untreated FALSE |
#>
#> [[2]]
#> # A SummarizedExperiment-tibble abstraction: Features=11886 | Samples=4 |
#> # Assays=counts
#> # |------------ COVARIATES -----------|
#> .feature .sample | counts | type condition `counts > 0` |
#> <chr> <chr> | <chr> | <chr> <chr> <chr> |
#> 1 FBgn0000008 untrt1 | 92 | single_end untreated TRUE |
#> 2 FBgn0000014 untrt1 | 5 | single_end untreated TRUE |
#> 3 FBgn0000017 untrt1 | 4664 | single_end untreated TRUE |
#> 4 FBgn0000018 untrt1 | 583 | single_end untreated TRUE |
#> 5 FBgn0000024 untrt1 | 10 | single_end untreated TRUE |
#> -------- ------- - ------ - ---- --------- ---------- -
#> 47540 FBgn0260430 untrt4 | 1 | paired_end untreated TRUE |
#> 47541 FBgn0260474 untrt4 | 1 | paired_end untreated TRUE |
#> 47542 FBgn0260867 untrt4 | 1 | paired_end untreated TRUE |
#> 47543 FBgn0261361 untrt4 | 1 | paired_end untreated TRUE |
#> 47544 FBgn0261523 untrt4 | 1 | paired_end untreated TRUE |
#>
#> [[3]]
#> # A SummarizedExperiment-tibble abstraction: Features=4990 | Samples=3 |
#> # Assays=counts
#> # |------------ COVARIATES -----------|
#> .feature .sample | counts | type condition `counts > 0` |
#> <chr> <chr> | <chr> | <chr> <chr> <chr> |
#> 1 FBgn0000003 trt1 | 0 | single_end treated FALSE |
#> 2 FBgn0000022 trt1 | 0 | single_end treated FALSE |
#> 3 FBgn0000028 trt1 | 0 | single_end treated FALSE |
#> 4 FBgn0000038 trt1 | 0 | single_end treated FALSE |
#> 5 FBgn0000039 trt1 | 0 | single_end treated FALSE |
#> -------- ------- - ------ - ---- --------- ---------- -
#> 14966 FBgn0261058 trt3 | 0 | paired_end treated FALSE |
#> 14967 FBgn0261356 trt3 | 0 | paired_end treated FALSE |
#> 14968 FBgn0261507 trt3 | 0 | paired_end treated FALSE |
#> 14969 FBgn0261508 trt3 | 0 | paired_end treated FALSE |
#> 14970 FBgn0261514 trt3 | 0 | paired_end treated FALSE |
#>
#> [[4]]
#> # A SummarizedExperiment-tibble abstraction: Features=11730 | Samples=3 |
#> # Assays=counts
#> # |------------ COVARIATES -----------|
#> .feature .sample | counts | type condition `counts > 0` |
#> <chr> <chr> | <chr> | <chr> <chr> <chr> |
#> 1 FBgn0000008 trt1 | 140 | single_end treated TRUE |
#> 2 FBgn0000014 trt1 | 4 | single_end treated TRUE |
#> 3 FBgn0000015 trt1 | 1 | single_end treated TRUE |
#> 4 FBgn0000017 trt1 | 6205 | single_end treated TRUE |
#> 5 FBgn0000018 trt1 | 722 | single_end treated TRUE |
#> -------- ------- - ------ - ---- --------- ---------- -
#> 35186 FBgn0259998 trt3 | 1 | paired_end treated TRUE |
#> 35187 FBgn0260996 trt3 | 1 | paired_end treated TRUE |
#> 35188 FBgn0261360 trt3 | 1 | paired_end treated TRUE |
#> 35189 FBgn0261401 trt3 | 1 | paired_end treated TRUE |
#> 35190 FBgn0261568 trt3 | 1 | paired_end treated TRUE |
#>