to_integer {fixest} | R Documentation |
Fast transform of any type of vector(s) into an integer vector
Description
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.
Usage
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 |
add_items |
Logical, default is |
items.list |
Logical, default is |
multi.df |
Logical, default is |
multi.join |
Character scalar used to join the items of multiple vectors.
The default is |
internal |
Logical, default is |
Value
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.
Author(s)
Laurent Berge
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 = "; ")