This function results in a two column data frame with nested structure. Currently only one parent is supported and child is only specified by giving the number of levels. (This will change shortly).

nest_in(
  x,
  ...,
  prefix = "",
  suffix = "",
  distinct = FALSE,
  leading0 = FALSE,
  compact = TRUE,
  keyname = NULL
)

Arguments

x

A vector where each entry is the level of a parent. It may be a factor or character. If character, levels are ordered alphanumerically.

...

A single integer, character vector or sequence of two-sided formula. If a single integer or character vector then each parent will have children specified by the given value. If it is sequence of two-sided formula, then the left hand side (LHS) specifies the level as an integer or character. E.g. 1 means the first level of the parent vector. If it is a character then it is assumed that it corresponds to the label of the parental level. Vector is supported for LHS. The right hand side (RHS) can only be an integer or a character vector.

prefix

The prefix for the child labels.

suffix

The suffix for the child labels.

distinct

A logical value to indicate whether the child labels across parents should be distinct. The labels are only distinct if the RHS of the formula is numeric.

leading0

By default it is FALSE. If TRUE, this is the same as setting 0 or 1. If a positive integer is specified then it corresponds to the minimum number of digits for the child labels and there will be leading zeros augmented so that the minimum number is met.

compact

A logical value to indicate whether the returned list should be a compact representation or not. Ignored if distinct is TRUE since it's not possible to make compact representation if unit labels are all distinct.

keyname

The name of the parent variable. It's usually the key that connects the output to another table.

Value

A named list where the entry corresponding to the child levels and the names correspond to parental levels.

Examples

# Each element in the supplied the vector has 4 child. nest_in(1:3, 4)
#> $`1` #> [1] "1" "2" "3" "4" #> #> $`2` #> [1] "1" "2" "3" "4" #> #> $`3` #> [1] "1" "2" "3" "4" #>
# prefix and suffix can be added to child labels # along with other aesthesitics like leading zeroes # with minimum number of digits. nest_in(1:3, 10, prefix = "id-", suffix = "xy", leading0 = 4)
#> $`1` #> [1] "id-0001xy" "id-0002xy" "id-0003xy" "id-0004xy" "id-0005xy" "id-0006xy" #> [7] "id-0007xy" "id-0008xy" "id-0009xy" "id-0010xy" #> #> $`2` #> [1] "id-0001xy" "id-0002xy" "id-0003xy" "id-0004xy" "id-0005xy" "id-0006xy" #> [7] "id-0007xy" "id-0008xy" "id-0009xy" "id-0010xy" #> #> $`3` #> [1] "id-0001xy" "id-0002xy" "id-0003xy" "id-0004xy" "id-0005xy" "id-0006xy" #> [7] "id-0007xy" "id-0008xy" "id-0009xy" "id-0010xy" #>
# you can specify unbalanced nested structures nest_in(2:4, 1 ~ 3, 2 ~ 4, 3 ~ 2)
#> $`2` #> [1] "1" "2" "3" #> #> $`3` #> [1] "1" "2" "3" "4" #> #> $`4` #> [1] "1" "2" #>
# A `.` may be used to specify "otherwise". nest_in(c("A", "B", "C", "D"), 2:3 ~ 10, . ~ 3)
#> $A #> [1] "1" "2" "3" #> #> $B #> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" #> #> $C #> [1] "1" "2" "3" #> #> $D #> [1] "1" "2" "3" #>
# The parental level can be referred by its name or vectorised. nest_in(c("A", "B", "C"), c("A", "B") ~ 10, "C" ~ 3)
#> $A #> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" #> #> $B #> NULL #> #> $C #> [1] "1" "2" "3" #>