to_integer function

Fast transform of any type of vector(s) into an integer vector

Fast transform of any type of vector(s) into an integer vector

Tool to transform any type of vector, or even combination of vectors, into an integer vector ranging from 1 to the number of unique values. This actually creates an unique identifier vector.

to_integer( ..., sorted = FALSE, add_items = FALSE, items.list = FALSE, multi.df = FALSE, multi.join = "_", internal = FALSE )

Arguments

  • ...: Vectors of any type, to be transformed in integer.
  • sorted: Logical, default is FALSE. Whether the integer vector should make reference to sorted values?
  • add_items: Logical, default is FALSE. Whether to add the unique values of the original vector(s). If requested, an attribute items is created containing the values (alternatively, they can appear in a list if items.list=TRUE).
  • items.list: Logical, default is FALSE. Only used if add_items=TRUE. If TRUE, then a list of length 2 is returned with x the integer vector and items the vector of items.
  • multi.df: Logical, default is FALSE. If TRUE then a data.frame listing the unique elements is returned in the form of a data.frame. Ignored if add_items = FALSE.
  • multi.join: Character scalar used to join the items of multiple vectors. The default is "_". Ignored if add_items = FALSE.
  • internal: Logical, default is FALSE. For programming only. If this function is used within another function, setting internal = TRUE is needed to make the evaluation of ... valid. End users of to_integer should not care.

Returns

Reruns a vector of the same length as the input vectors. If add_items=TRUE and items.list=TRUE, a list of two elements is returned: x

being the integer vector and items being the unique values to which the values in x make reference.

Examples

x1 = iris$Species x2 = as.integer(iris$Sepal.Length) # transforms the species vector into integers to_integer(x1) # To obtain the "items": to_integer(x1, add_items = TRUE) # same but in list form to_integer(x1, add_items = TRUE, items.list = TRUE) # transforms x2 into an integer vector from 1 to 4 to_integer(x2, add_items = TRUE) # To have the sorted items: to_integer(x2, add_items = TRUE, sorted = TRUE) # The result can safely be used as an index res = to_integer(x2, add_items = TRUE, sorted = TRUE, items.list = TRUE) all(res$items[res$x] == x2) # # Multiple vectors # to_integer(x1, x2, add_items = TRUE) # You can use multi.join to handle the join of the items: to_integer(x1, x2, add_items = TRUE, multi.join = "; ")

Author(s)

Laurent Berge