Skip to contents

Split by factor level

Usage

splitByLevel(x, f, ref = FALSE, ...)

# S4 method for DFrame
splitByLevel(x, f, ref = FALSE)

Arguments

x

Object.

f

character(1). Factor column name.

ref

logical(1). Include the reference factor level (i.e. first defined) in each split. Useful for pairwise contrasts.

...

Additional arguments.

Value

list

List of data frames split by factors.

Note

Updated 2020-09-14.

Examples

df <- S4Vectors::DataFrame(
    "compound" = relevel(
        factor(
            c(
                rep(x = "dmso", each = 3L),
                rep(x = c("ts_0001", "ts_0002"), each = 9L)
            )
        ),
        ref = "dmso"
    ),
    "concentration" = factor(
        c(
            rep(x = 0, each = 3L),
            rep(x = c(0.1, 1, 10), each = 3L, times = 2L)
        )
    ),
    "replicate" = factor(
        rep(
            seq(from = 1L, to = 3L),
            times = 7L
        )
    )
)
lapply(df, levels)
#> $compound
#> [1] "dmso"    "ts_0001" "ts_0002"
#> 
#> $concentration
#> [1] "0"   "0.1" "1"   "10" 
#> 
#> $replicate
#> [1] "1" "2" "3"
#> 
splitByLevel(df, f = "compound", ref = FALSE)
#> [[1]]
#> DataFrame with 3 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1     dmso             0         1
#> 2     dmso             0         2
#> 3     dmso             0         3
#> 
#> [[2]]
#> DataFrame with 9 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  ts_0001           0.1         1
#> 2  ts_0001           0.1         2
#> 3  ts_0001           0.1         3
#> 4  ts_0001           1           1
#> 5  ts_0001           1           2
#> 6  ts_0001           1           3
#> 7  ts_0001           10          1
#> 8  ts_0001           10          2
#> 9  ts_0001           10          3
#> 
#> [[3]]
#> DataFrame with 9 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  ts_0002           0.1         1
#> 2  ts_0002           0.1         2
#> 3  ts_0002           0.1         3
#> 4  ts_0002           1           1
#> 5  ts_0002           1           2
#> 6  ts_0002           1           3
#> 7  ts_0002           10          1
#> 8  ts_0002           10          2
#> 9  ts_0002           10          3
#> 
splitByLevel(df, f = "compound", ref = TRUE)
#> [[1]]
#> DataFrame with 12 rows and 3 columns
#>     compound concentration replicate
#>     <factor>      <factor>  <factor>
#> 1    dmso              0           1
#> 2    dmso              0           2
#> 3    dmso              0           3
#> 4    ts_0001           0.1         1
#> 5    ts_0001           0.1         2
#> ...      ...           ...       ...
#> 8    ts_0001            1          2
#> 9    ts_0001            1          3
#> 10   ts_0001            10         1
#> 11   ts_0001            10         2
#> 12   ts_0001            10         3
#> 
#> [[2]]
#> DataFrame with 12 rows and 3 columns
#>     compound concentration replicate
#>     <factor>      <factor>  <factor>
#> 1    dmso              0           1
#> 2    dmso              0           2
#> 3    dmso              0           3
#> 4    ts_0002           0.1         1
#> 5    ts_0002           0.1         2
#> ...      ...           ...       ...
#> 8    ts_0002            1          2
#> 9    ts_0002            1          3
#> 10   ts_0002            10         1
#> 11   ts_0002            10         2
#> 12   ts_0002            10         3
#> 
splitByLevel(df, f = "concentration", ref = FALSE)
#> [[1]]
#> DataFrame with 3 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1     dmso             0         1
#> 2     dmso             0         2
#> 3     dmso             0         3
#> 
#> [[2]]
#> DataFrame with 6 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  ts_0001           0.1         1
#> 2  ts_0001           0.1         2
#> 3  ts_0001           0.1         3
#> 4  ts_0002           0.1         1
#> 5  ts_0002           0.1         2
#> 6  ts_0002           0.1         3
#> 
#> [[3]]
#> DataFrame with 6 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  ts_0001             1         1
#> 2  ts_0001             1         2
#> 3  ts_0001             1         3
#> 4  ts_0002             1         1
#> 5  ts_0002             1         2
#> 6  ts_0002             1         3
#> 
#> [[4]]
#> DataFrame with 6 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  ts_0001            10         1
#> 2  ts_0001            10         2
#> 3  ts_0001            10         3
#> 4  ts_0002            10         1
#> 5  ts_0002            10         2
#> 6  ts_0002            10         3
#> 
splitByLevel(df, f = "concentration", ref = TRUE)
#> [[1]]
#> DataFrame with 9 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  dmso              0           1
#> 2  dmso              0           2
#> 3  dmso              0           3
#> 4  ts_0001           0.1         1
#> 5  ts_0001           0.1         2
#> 6  ts_0001           0.1         3
#> 7  ts_0002           0.1         1
#> 8  ts_0002           0.1         2
#> 9  ts_0002           0.1         3
#> 
#> [[2]]
#> DataFrame with 9 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  dmso                0         1
#> 2  dmso                0         2
#> 3  dmso                0         3
#> 4  ts_0001             1         1
#> 5  ts_0001             1         2
#> 6  ts_0001             1         3
#> 7  ts_0002             1         1
#> 8  ts_0002             1         2
#> 9  ts_0002             1         3
#> 
#> [[3]]
#> DataFrame with 9 rows and 3 columns
#>   compound concentration replicate
#>   <factor>      <factor>  <factor>
#> 1  dmso               0          1
#> 2  dmso               0          2
#> 3  dmso               0          3
#> 4  ts_0001            10         1
#> 5  ts_0001            10         2
#> 6  ts_0001            10         3
#> 7  ts_0002            10         1
#> 8  ts_0002            10         2
#> 9  ts_0002            10         3
#>