#' Assign an id to a list of characters or factors
#'
#' This function aims to assign an id to factor or similar character
#' strings. Regarding factors, [as.integer()] does such task but the order
#' obtained may differ.
#'
#' @param x an R object to be coerced into character type.
#' @param alphabetical a logical indicating whether an alphabetical sorting
#' must be applied.
#' @return
#' A vector of Ids.
#' @export
#' @examples
#' assignIds(list(2,'f', 'd', 'f'))
assignIds <- function(x, alphabetical = FALSE) {
tmp <- unique(as.character(x))
if (alphabetical)
tmp <- sort(tmp)
out <- integer(length(tmp))
for (i in seq_along(tmp)) out[x == tmp[i]] <- i
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.