Var.as_factor(labels='%r', name=None, random=False)

Convert the Var into a Factor

  • labels (str | dict) – Either a format string for converting values into labels (default: '%r') or a dictionary mapping values to labels (see examples). In a dictionary, multiple values can be assigned the same label by providing multiple keys in a tuple. A special key ‘default’ can be used to assign values that are not otherwise specified in the dictionary (by default this is the empty string '').

  • name (str) – Name of the output Factor (default is the current name).

  • random (bool) – Whether the Factor is a random Factor (default False).


>>> v = Var([0, 1, 2, 3])
>>> v.as_factor()
Factor(['0', '1', '2', '3'])
>>> v.as_factor({0: 'a', 1: 'b'})
Factor(['a', 'b', '', ''])
>>> v.as_factor({(0, 1): 'a', (2, 3): 'b'})
Factor(['a', 'a', 'b', 'b'])
>>> v.as_factor({0: 'a', 1: 'b', 'default': 'c'})
Factor(['a', 'b', 'c', 'c'])