Skip to contents

Ease various specifications for fac specification when passed to Momocs objects. Intensively used (internally).

Usage

fac_dispatcher(x, fac)

Arguments

x

a Momocs object (any Coo, Coe, PCA, etc.)

fac

a specification to extract from fac

Value

a prepared factor (or a numeric). See examples

Details

fac can be:

  • a factor, passed on the fly

  • a column id from $fac

  • a column name from fac; if not found, return NULL with a message

  • a formula in the form: ~column_name (from $fac, no quotes). It expresses more in a concise way. Also allows interacting on the fly. See examples.

  • a NULL returns a NULL, with a message

See also

Other handling functions: arrange(), at_least(), chop(), combine(), dissolve(), filter(), mutate(), rename(), rescale(), rm_harm(), rm_missing(), rm_uncomplete(), rw_fac(), sample_frac(), sample_n(), select(), slice(), subsetize()

Examples


bot <- mutate(bot, s=rnorm(40), fake=factor(rep(letters[1:4], 10)))

# factor, on the fly
fac_dispatcher(bot, factor(rep(letters[1:4], 10)))
#>  [1] a b c d a b c d a b c d a b c d a b c d a b c d a b c d a b c d a b c d a b
#> [39] c d
#> Levels: a b c d

# column id
fac_dispatcher(bot, 1)
#>  type1  type2  type3  type4  type5  type6  type7  type8  type9 type10 type11 
#> whisky whisky whisky whisky whisky whisky whisky whisky whisky whisky whisky 
#> type12 type13 type14 type15 type16 type17 type18 type19 type20 type21 type22 
#> whisky whisky whisky whisky whisky whisky whisky whisky whisky   beer   beer 
#> type23 type24 type25 type26 type27 type28 type29 type30 type31 type32 type33 
#>   beer   beer   beer   beer   beer   beer   beer   beer   beer   beer   beer 
#> type34 type35 type36 type37 type38 type39 type40 
#>   beer   beer   beer   beer   beer   beer   beer 
#> Levels: beer whisky

# column name
fac_dispatcher(bot, "type")
#>  type1  type2  type3  type4  type5  type6  type7  type8  type9 type10 type11 
#> whisky whisky whisky whisky whisky whisky whisky whisky whisky whisky whisky 
#> type12 type13 type14 type15 type16 type17 type18 type19 type20 type21 type22 
#> whisky whisky whisky whisky whisky whisky whisky whisky whisky   beer   beer 
#> type23 type24 type25 type26 type27 type28 type29 type30 type31 type32 type33 
#>   beer   beer   beer   beer   beer   beer   beer   beer   beer   beer   beer 
#> type34 type35 type36 type37 type38 type39 type40 
#>   beer   beer   beer   beer   beer   beer   beer 
#> Levels: beer whisky
# same, numeric case
fac_dispatcher(bot, "s")
#>           s1           s2           s3           s4           s5           s6 
#>  1.708650899 -0.759601384  0.972369565 -1.031840383  0.768586627  0.301285060 
#>           s7           s8           s9          s10          s11          s12 
#>  0.424560581  0.006571057  0.741297749  1.559007487 -0.417168752 -0.420322963 
#>          s13          s14          s15          s16          s17          s18 
#>  0.697185864 -0.210961609  1.541085498  0.284831960  0.321961376 -1.068526069 
#>          s19          s20          s21          s22          s23          s24 
#>  1.425715862  1.199570200 -1.678036407  1.828806163 -0.535208856 -0.835660641 
#>          s25          s26          s27          s28          s29          s30 
#> -0.758469900 -1.324129902  0.788467471 -1.376718465 -1.070395242 -0.986740931 
#>          s31          s32          s33          s34          s35          s36 
#>  0.536586184 -0.445206584 -0.003501437 -0.623814095  0.460846925 -0.577520120 
#>          s37          s38          s39          s40 
#>  1.687608566  0.025660517 -0.590307162  1.382021803 

# formula interface
fac_dispatcher(bot, ~type)
#>  type1  type2  type3  type4  type5  type6  type7  type8  type9 type10 type11 
#> whisky whisky whisky whisky whisky whisky whisky whisky whisky whisky whisky 
#> type12 type13 type14 type15 type16 type17 type18 type19 type20 type21 type22 
#> whisky whisky whisky whisky whisky whisky whisky whisky whisky   beer   beer 
#> type23 type24 type25 type26 type27 type28 type29 type30 type31 type32 type33 
#>   beer   beer   beer   beer   beer   beer   beer   beer   beer   beer   beer 
#> type34 type35 type36 type37 type38 type39 type40 
#>   beer   beer   beer   beer   beer   beer   beer 
#> Levels: beer whisky

# formula interface + interaction on the fly
fac_dispatcher(bot, ~type+fake)
#>  [1] whisky_a whisky_b whisky_c whisky_d whisky_a whisky_b whisky_c whisky_d
#>  [9] whisky_a whisky_b whisky_c whisky_d whisky_a whisky_b whisky_c whisky_d
#> [17] whisky_a whisky_b whisky_c whisky_d beer_a   beer_b   beer_c   beer_d  
#> [25] beer_a   beer_b   beer_c   beer_d   beer_a   beer_b   beer_c   beer_d  
#> [33] beer_a   beer_b   beer_c   beer_d   beer_a   beer_b   beer_c   beer_d  
#> Levels: beer_a beer_b beer_c beer_d whisky_a whisky_b whisky_c whisky_d

# when passing NULL or non existing column
fac_dispatcher(42, NULL)
#> NULL
fac_dispatcher(bot, "loser")
#> not a valid column specification, returning NULL
#> NULL