This function has a similar result with split()
where
it returns a named list with names corresponding to the
levels of the separating factor (or concatenated strings
if multiple separating factors). The key differences to split()
,
are that the splitting factor does not appear in the elements of the
list and only linked factors and their ancestors appear in the output, e.g.
if treatment is applied to wholeplot and subplots are nested within
subplots, then the subplot will not be shown in the output if split by
treatment.
Usage
split_by(.data, ..., .sep = ":", .remove_empty = TRUE)
count_by(.data, ..., .remove_empty = TRUE)
Arguments
- .data
An edibble table.
- ...
The factors to split or count by. You cannot split by a record factor or a factor that uniquely indexes the smallest unit in the design. You cannot also combine treatment and unit factors together.
- .sep
The separator to use if more than one factor to split by.
- .remove_empty
Remove empty combinations. Default is TRUE.
Examples
spd <- takeout(menu_split())
split(spd, spd$trt1)
#> $trt11
#> design("Split-Plot Design | Split-Unit Design") %>%
#> set_units(mainplot = 10,
#> subplot = nested_in(mainplot, 4)) %>%
#> set_trts(trt1 = 5,
#> trt2 = 4) %>%
#> allot_trts(trt1 ~ mainplot,
#> trt2 ~ subplot) %>%
#> assign_trts("random", seed = 574) %>%
#> serve_table()
#>
#> # Split-Plot Design | Split-Unit Design
#> # An edibble: 8 x 4
#> mainplot subplot trt1 trt2
#> <U(10)> <U(40)> <T(5)> <T(4)>
#> <chr> <chr> <chr> <chr>
#> 1 mainplot06 subplot21 trt11 trt21
#> 2 mainplot06 subplot22 trt11 trt23
#> 3 mainplot06 subplot23 trt11 trt24
#> 4 mainplot06 subplot24 trt11 trt22
#> 5 mainplot07 subplot25 trt11 trt23
#> 6 mainplot07 subplot26 trt11 trt24
#> 7 mainplot07 subplot27 trt11 trt22
#> 8 mainplot07 subplot28 trt11 trt21
#>
#> $trt12
#> design("Split-Plot Design | Split-Unit Design") %>%
#> set_units(mainplot = 10,
#> subplot = nested_in(mainplot, 4)) %>%
#> set_trts(trt1 = 5,
#> trt2 = 4) %>%
#> allot_trts(trt1 ~ mainplot,
#> trt2 ~ subplot) %>%
#> assign_trts("random", seed = 574) %>%
#> serve_table()
#>
#> # Split-Plot Design | Split-Unit Design
#> # An edibble: 8 x 4
#> mainplot subplot trt1 trt2
#> <U(10)> <U(40)> <T(5)> <T(4)>
#> <chr> <chr> <chr> <chr>
#> 1 mainplot01 subplot01 trt12 trt23
#> 2 mainplot01 subplot02 trt12 trt21
#> 3 mainplot01 subplot03 trt12 trt24
#> 4 mainplot01 subplot04 trt12 trt22
#> 5 mainplot05 subplot17 trt12 trt21
#> 6 mainplot05 subplot18 trt12 trt24
#> 7 mainplot05 subplot19 trt12 trt22
#> 8 mainplot05 subplot20 trt12 trt23
#>
#> $trt13
#> design("Split-Plot Design | Split-Unit Design") %>%
#> set_units(mainplot = 10,
#> subplot = nested_in(mainplot, 4)) %>%
#> set_trts(trt1 = 5,
#> trt2 = 4) %>%
#> allot_trts(trt1 ~ mainplot,
#> trt2 ~ subplot) %>%
#> assign_trts("random", seed = 574) %>%
#> serve_table()
#>
#> # Split-Plot Design | Split-Unit Design
#> # An edibble: 8 x 4
#> mainplot subplot trt1 trt2
#> <U(10)> <U(40)> <T(5)> <T(4)>
#> <chr> <chr> <chr> <chr>
#> 1 mainplot02 subplot05 trt13 trt24
#> 2 mainplot02 subplot06 trt13 trt21
#> 3 mainplot02 subplot07 trt13 trt22
#> 4 mainplot02 subplot08 trt13 trt23
#> 5 mainplot09 subplot33 trt13 trt21
#> 6 mainplot09 subplot34 trt13 trt22
#> 7 mainplot09 subplot35 trt13 trt23
#> 8 mainplot09 subplot36 trt13 trt24
#>
#> $trt14
#> design("Split-Plot Design | Split-Unit Design") %>%
#> set_units(mainplot = 10,
#> subplot = nested_in(mainplot, 4)) %>%
#> set_trts(trt1 = 5,
#> trt2 = 4) %>%
#> allot_trts(trt1 ~ mainplot,
#> trt2 ~ subplot) %>%
#> assign_trts("random", seed = 574) %>%
#> serve_table()
#>
#> # Split-Plot Design | Split-Unit Design
#> # An edibble: 8 x 4
#> mainplot subplot trt1 trt2
#> <U(10)> <U(40)> <T(5)> <T(4)>
#> <chr> <chr> <chr> <chr>
#> 1 mainplot03 subplot09 trt14 trt23
#> 2 mainplot03 subplot10 trt14 trt24
#> 3 mainplot03 subplot11 trt14 trt21
#> 4 mainplot03 subplot12 trt14 trt22
#> 5 mainplot10 subplot37 trt14 trt23
#> 6 mainplot10 subplot38 trt14 trt21
#> 7 mainplot10 subplot39 trt14 trt24
#> 8 mainplot10 subplot40 trt14 trt22
#>
#> $trt15
#> design("Split-Plot Design | Split-Unit Design") %>%
#> set_units(mainplot = 10,
#> subplot = nested_in(mainplot, 4)) %>%
#> set_trts(trt1 = 5,
#> trt2 = 4) %>%
#> allot_trts(trt1 ~ mainplot,
#> trt2 ~ subplot) %>%
#> assign_trts("random", seed = 574) %>%
#> serve_table()
#>
#> # Split-Plot Design | Split-Unit Design
#> # An edibble: 8 x 4
#> mainplot subplot trt1 trt2
#> <U(10)> <U(40)> <T(5)> <T(4)>
#> <chr> <chr> <chr> <chr>
#> 1 mainplot04 subplot13 trt15 trt22
#> 2 mainplot04 subplot14 trt15 trt23
#> 3 mainplot04 subplot15 trt15 trt24
#> 4 mainplot04 subplot16 trt15 trt21
#> 5 mainplot08 subplot29 trt15 trt22
#> 6 mainplot08 subplot30 trt15 trt21
#> 7 mainplot08 subplot31 trt15 trt24
#> 8 mainplot08 subplot32 trt15 trt23
#>
spd %>% split_by(trt1)
#> $trt11
#> # A tibble: 2 × 1
#> mainplot
#> <U(10)>
#> 1 mainplot06
#> 2 mainplot07
#>
#> $trt12
#> # A tibble: 2 × 1
#> mainplot
#> <U(10)>
#> 1 mainplot01
#> 2 mainplot05
#>
#> $trt13
#> # A tibble: 2 × 1
#> mainplot
#> <U(10)>
#> 1 mainplot02
#> 2 mainplot09
#>
#> $trt14
#> # A tibble: 2 × 1
#> mainplot
#> <U(10)>
#> 1 mainplot03
#> 2 mainplot10
#>
#> $trt15
#> # A tibble: 2 × 1
#> mainplot
#> <U(10)>
#> 1 mainplot04
#> 2 mainplot08
#>
spd %>% split_by(trt2)
#> $trt21
#> # A tibble: 10 × 2
#> subplot mainplot
#> <U(40)> <U(10)>
#> 1 subplot02 mainplot01
#> 2 subplot06 mainplot02
#> 3 subplot11 mainplot03
#> 4 subplot16 mainplot04
#> 5 subplot17 mainplot05
#> 6 subplot21 mainplot06
#> 7 subplot28 mainplot07
#> 8 subplot30 mainplot08
#> 9 subplot33 mainplot09
#> 10 subplot38 mainplot10
#>
#> $trt22
#> # A tibble: 10 × 2
#> subplot mainplot
#> <U(40)> <U(10)>
#> 1 subplot04 mainplot01
#> 2 subplot07 mainplot02
#> 3 subplot12 mainplot03
#> 4 subplot13 mainplot04
#> 5 subplot19 mainplot05
#> 6 subplot24 mainplot06
#> 7 subplot27 mainplot07
#> 8 subplot29 mainplot08
#> 9 subplot34 mainplot09
#> 10 subplot40 mainplot10
#>
#> $trt23
#> # A tibble: 10 × 2
#> subplot mainplot
#> <U(40)> <U(10)>
#> 1 subplot01 mainplot01
#> 2 subplot08 mainplot02
#> 3 subplot09 mainplot03
#> 4 subplot14 mainplot04
#> 5 subplot20 mainplot05
#> 6 subplot22 mainplot06
#> 7 subplot25 mainplot07
#> 8 subplot32 mainplot08
#> 9 subplot35 mainplot09
#> 10 subplot37 mainplot10
#>
#> $trt24
#> # A tibble: 10 × 2
#> subplot mainplot
#> <U(40)> <U(10)>
#> 1 subplot03 mainplot01
#> 2 subplot05 mainplot02
#> 3 subplot10 mainplot03
#> 4 subplot15 mainplot04
#> 5 subplot18 mainplot05
#> 6 subplot23 mainplot06
#> 7 subplot26 mainplot07
#> 8 subplot31 mainplot08
#> 9 subplot36 mainplot09
#> 10 subplot39 mainplot10
#>
spd %>% split_by(mainplot)
#> $mainplot01
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot01 trt23
#> 2 subplot02 trt21
#> 3 subplot03 trt24
#> 4 subplot04 trt22
#>
#> $mainplot02
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot05 trt24
#> 2 subplot06 trt21
#> 3 subplot07 trt22
#> 4 subplot08 trt23
#>
#> $mainplot03
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot09 trt23
#> 2 subplot10 trt24
#> 3 subplot11 trt21
#> 4 subplot12 trt22
#>
#> $mainplot04
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot13 trt22
#> 2 subplot14 trt23
#> 3 subplot15 trt24
#> 4 subplot16 trt21
#>
#> $mainplot05
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot17 trt21
#> 2 subplot18 trt24
#> 3 subplot19 trt22
#> 4 subplot20 trt23
#>
#> $mainplot06
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot21 trt21
#> 2 subplot22 trt23
#> 3 subplot23 trt24
#> 4 subplot24 trt22
#>
#> $mainplot07
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot25 trt23
#> 2 subplot26 trt24
#> 3 subplot27 trt22
#> 4 subplot28 trt21
#>
#> $mainplot08
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot29 trt22
#> 2 subplot30 trt21
#> 3 subplot31 trt24
#> 4 subplot32 trt23
#>
#> $mainplot09
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot33 trt21
#> 2 subplot34 trt22
#> 3 subplot35 trt23
#> 4 subplot36 trt24
#>
#> $mainplot10
#> # A tibble: 4 × 2
#> subplot trt2
#> <U(40)> <T(4)>
#> 1 subplot37 trt23
#> 2 subplot38 trt21
#> 3 subplot39 trt24
#> 4 subplot40 trt22
#>
spd %>% count_by(trt1)
#> trt1 mainplot
#> 1 trt11 2
#> 2 trt12 2
#> 3 trt13 2
#> 4 trt14 2
#> 5 trt15 2
fac <- takeout(menu_factorial(trt = c(2, 2, 2)))
fac %>% count_by(where(~is_trt(.x)))
#> trt1 trt2 trt3 unit
#> 1 trt11 trt21 trt31 4
#> 2 trt12 trt21 trt31 4
#> 3 trt11 trt22 trt31 4
#> 4 trt12 trt22 trt31 4
#> 5 trt11 trt21 trt32 4
#> 6 trt12 trt21 trt32 4
#> 7 trt11 trt22 trt32 4
#> 8 trt12 trt22 trt32 4