R/f2int.R

Defines functions f2int

f2int <- function(x, type = 1L) 
{
  if(!is.factor(x))
    x <- as.factor(x)
  if(type != 3L) {
    levels(x) <- nl <- 1:nlevels(x)
    x <- factor(x, levels = nl, labels = nl)
    x <- as.integer(x)
    if(type != 2L)
      x <- x - 1L
    if(min(x, na.rm = TRUE) < 0)
      x <- x + abs(min(x, na.rm = TRUE))
  } else {
    warn <- getOption("warn")
    options("warn" = -1)
    x2 <- as.integer(as.character(x))
    x <- if(all(is.na(x2))) as.integer(x) else x2
    options("warn" = warn)
  }

  return(x)
}

Try the R2BayesX package in your browser

Any scripts or data that you put into this service are public.

R2BayesX documentation built on Oct. 20, 2023, 3:01 p.m.