R/convert.R

## author: Olivia Lau
## date  : Dec, 2007
convert <- function(vars, data, order, ...) {
  vid <- unique(c(as.character(data[[match(vars, names(data))]])))
  if (!identical(sort(na.omit(order)), sort(na.omit(vid)))) {
    stop("all values must be identified in order")
  }
  if (NA %in% order) keepna <- NULL
  else keepna <- NA
  for (i in vars) {
    tmp <- data[[i]]
    tmp <- unclass(factor(tmp, levels = order, exclude = keepna, ...))
    data[[i]] <- tmp
   attr(data[[i]], "levels") <- NULL
  }
  data
}

Try the anchors package in your browser

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

anchors documentation built on May 2, 2019, 6:59 a.m.