Split a Pop-class or MultiPop-class object
into a MultiPop using one or more grouping specifications passed
through by.
Arguments
- x
a
Pop-classorMultiPop-classobject- by
a vector, function, list, or data frame defining groupings
- level
a positive integer, a vector of positive integers, or
Inf. Only relevant whenxis aMultiPop-classobject. See Details.
Value
Returns a MultiPop-class object.
Details
The by argument can take several forms:
- Atomic vectors: used directly as grouping labels. The
vector is passed to split and may be recycled as
needed. If the vector length is not a multiple of the population size,
split issues a warning.
- Functions: called on each Pop object and must return
an atomic vector of grouping labels.
- Lists: each element must be a vector or function. Elements
are applied recursively to create nested MultiPop-class
objects.
- Data frames: each column defines one nesting level. In this
case, x must be a Pop-class object. Rows of the data
frame must correspond to individuals in x. If row names are present
and match x@id, they are used to align rows to individuals;
otherwise rows are assumed to be in population order and a warning is
issued. NA values are allowed in grouping columns and can be used
to represent uneven nesting structures. If all grouping values for a
subpopulation are NA at a given level, splitting stops for that
branch.
The level argument is only relevant when x is a
MultiPop-class object. If level = Inf, all
Pop-class objects contained in x are split according
to by. If, for example, level=c(1,3), only
Pop-class objects at nesting levels 1 and 3 are
split, where the top level is defined as level 1. An error is raised
if any specified level exceeds the maximum nesting depth of x.
Examples
# Create founder haplotypes
founderPop = quickHaplo(nInd = 10, nChr = 1, segSites = 10)
# Set simulation parameters
SP = SimParam$new(founderPop)
SP$addTraitA(10)
# Create population
pop = newPop(founderPop, simParam = SP)
# Split pop into groups A/B deterministically
mp1 = splitPop(pop, by = rep(c("A", "B"), length.out = nInd(pop)))
mp1
#> An object of class "MultiPop" with 2 item(s)
#> Level 1:
#> |-- [[1]] "A" - An object of class "Pop" with 5 individual(s): 1, 3, ..., 9
#> `-- [[2]] "B" - An object of class "Pop" with 5 individual(s): 2, 4, ..., 10
# Split using a data frame, with rows aligned by individual ID
by_df = data.frame(
level1 = sample(LETTERS[1:3], nInd(pop), replace = TRUE),
level2 = sample(1:2, nInd(pop), replace = TRUE),
row.names = pop@id
)
splitPop(pop, by = by_df)
#> An object of class "MultiPop" with 3 item(s)
#> Level 1:
#> |-- [[1]] "A" - An object of class "MultiPop" with 2 item(s)
#> | | Level 2:
#> | |-- [[1]] "1" - An object of class "Pop" with 1 individual(s): 3
#> | `-- [[2]] "2" - An object of class "Pop" with 1 individual(s): 2
#> |-- [[2]] "B" - An object of class "MultiPop" with 2 item(s)
#> | | Level 2:
#> | |-- [[1]] "1" - An object of class "Pop" with 3 individual(s): 1, 4, 5
#> | `-- [[2]] "2" - An object of class "Pop" with 3 individual(s): 7, 8, 9
#> `-- [[3]] "C" - An object of class "MultiPop" with 1 item(s)
#> | Level 2:
#> `-- [[1]] "2" - An object of class "Pop" with 2 individual(s): 6, 10
# Uneven nested structure using NA values in lower levels
by_df2 = data.frame(
level1 = c(rep("A", 4), rep("B", 6)),
level2 = c(rep(NA_character_, 4), rep(c("C", "D"), each = 3)),
row.names = pop@id
)
splitPop(pop, by = by_df2)
#> An object of class "MultiPop" with 2 item(s)
#> Level 1:
#> |-- [[1]] "A" - An object of class "Pop" with 4 individual(s): 1, 2, ..., 4
#> `-- [[2]] "B" - An object of class "MultiPop" with 2 item(s)
#> | Level 2:
#> |-- [[1]] "C" - An object of class "Pop" with 3 individual(s): 5, 6, 7
#> `-- [[2]] "D" - An object of class "Pop" with 3 individual(s): 8, 9, 10
# Nested split: first by a random grouping vector, then by family
mp2 = splitPop(
pop,
by = list(
sample(LETTERS[1:3], nInd(pop), replace = TRUE),
function(x) paste(x@mother, x@father, sep = "_")
)
)
mp2
#> An object of class "MultiPop" with 3 item(s)
#> Level 1:
#> |-- [[1]] "A" - An object of class "MultiPop" with 1 item(s)
#> | | Level 2:
#> | `-- [[1]] "0_0" - An object of class "Pop" with 2 individual(s): 2, 9
#> |-- [[2]] "B" - An object of class "MultiPop" with 1 item(s)
#> | | Level 2:
#> | `-- [[1]] "0_0" - An object of class "Pop" with 4 individual(s): 1, 3, ..., 6
#> `-- [[3]] "C" - An object of class "MultiPop" with 1 item(s)
#> | Level 2:
#> `-- [[1]] "0_0" - An object of class "Pop" with 4 individual(s): 5, 7, ..., 10
# When x is a MultiPop, control which nesting levels are split
mp_nested = newMultiPop(pop[1:3], newMultiPop(pop[4:6], pop[7:9]))
splitPop(
mp_nested,
by = function(p) rep(c("A", "B"), length.out = nInd(p)),
level = 1
)
#> An object of class "MultiPop" with 2 item(s)
#> Level 1:
#> |-- [[1]] An object of class "MultiPop" with 2 item(s)
#> | | Level 2:
#> | |-- [[1]] "A" - An object of class "Pop" with 2 individual(s): 1, 3
#> | `-- [[2]] "B" - An object of class "Pop" with 1 individual(s): 2
#> `-- [[2]] An object of class "MultiPop" with 2 item(s)
#> | Level 2:
#> |-- [[1]] An object of class "Pop" with 3 individual(s): 4, 5, 6
#> `-- [[2]] An object of class "Pop" with 3 individual(s): 7, 8, 9