#' Set levels of a factor to the order appeared in data
#'
#' @param x a vector or a factor
#'
#' @return a factor with levels in the order appeared in x.
#' This function will only return unique x levels,
#'
#' @examples order_appeared (c("pear","apple","banana","banana","apple"))
#' @export
order_appeared <- function(x){
if(!(is.factor(x)||is.character(x))){
stop("This input is not character or factor")
}
unique_order<-as.character(unique(x))
result <- factor(x, levels = unique_order)
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.