| to_integer {fixest} | R Documentation |
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.join = FALSE )
... |
Vectors of any type, to be transformed in integer. |
sorted |
Logical, default is |
add_items |
Logical, default is |
items.list |
Logical, default is |
multi.join |
Logical, or character, scalar, defaults to |
If multiple vectors have to be combined and add_items=TRUE, to have user readable values in the items, you should add the argument multi.join so that the values of the vectors are combined in a "user-readable" way. Note that in the latter case, the algorithm is much much slower.
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.
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 # # by default, the two vector are fast combined, and items are meaningless to_integer(x1, x2, add_items = TRUE) # You can use multi.join to have human-readable values for the items: to_integer(x1, x2, add_items = TRUE, multi.join = TRUE) to_integer(x1, x2, add_items = TRUE, multi.join = "; ")