| to_integer | 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(
...,
inputs = NULL,
sorted = FALSE,
add_items = FALSE,
items.list = FALSE,
multi.df = FALSE,
multi.join = "_",
na.valid = FALSE,
internal = FALSE
)
... |
Vectors of any type, to be transformed into a single integer vector ranging from 1 to the number of unique elements. |
inputs |
A list of inputs, by default it is |
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 |
na.valid |
Logical, default is |
internal |
Logical, default is |
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.
Laurent Berge
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)
# placing the three side to side
head(cbind(x2, as_index = to_integer(x2),
as_index_sorted = to_integer(x2, 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 = "; ")
# alternatively, return the items as a data.frame
to_integer(x1, x2, add_items = TRUE, multi.df = TRUE)
#
# NA values
#
x1_na = c("a", "a", "b", NA, NA, "b", "a", "c", NA)
x2_na = c(NA, 1, NA, 1, 1, 1, 2, 2, 2)
# by default the NAs are propagated
to_integer(x1_na, x2_na, add_items = TRUE)
# but you can treat them as valid values with na.valid = TRUE
to_integer(x1_na, x2_na, add_items = TRUE, na.valid = TRUE)
#
# programmatic use
#
# the argument `inputs` can be used for easy programmatic use
all_vars = list(x1_na, x2_na)
to_integer(inputs = all_vars)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.