% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/recode.R
\name{recode}
\alias{recode}
\title{Recode the Values of obj from "from" to "to".}
\usage{
recode(
  obj,
  from = NULL,
  to = NULL,
  ft = NULL,
  method = 2,
  useNA = 0,
  debug = 0
)
}
\arguments{
\item{obj}{a vector, matrix or list of those.}

\item{from}{a vector of old values}

\item{to}{a vector or new values}

\item{ft}{the conversion table data frame}

\item{method}{= 1 to use \code{merge}, \cr
= 2 to use R style teble look-up using names attribute of a vector.}

\item{useNA}{= 1 to mark out-of-range elements}

\item{debug}{= 1 to print intermediate result.}
}
\value{
A matrix or vector depending on the \code{obj}.
 When \code{obj} is a list, a list will be returned.
}
\description{
Recode the Values of obj from "from" to "to".
}
\details{
When \code{ft} is not given,
 \code{cbind(from,to)}  makes the conversion table \code{ft}. \cr
 \code{ft} has priority over from and to.

 The core of this function is:\cr\cr
 If \code{method==1} \cr
 \code{New <- merge( cbind(from,to), data.frame(from=c(obj))
 , by="from", sort=FALSE, all.y=TRUE )} \cr
 If \code{method==2} \cr
 \code{names(to) <- from; New <- to[as.character(c(obj))]} \cr

 Method 2 is faster than method 1. \cr

 If the conversion table is not large enough,
 if \code{method=2} and \code{useNA=1},
 out-of-range elements will be set to NAs,
 otherwise, the original values will not  be changed.

 When the types of \code{obj} and \code{from} are different,
 they will be converted to character.

 \code{lazy.tools::new_name} will be deprecated.
}
\examples{
n <- 10
n1 <- n-2
obj <- cbind( letters[1:n], rev( letters[1:n] ) )
obj[1,1] <- NA; obj[3,2] <- NA
rownames(obj) <- paste("RR",1:n,sep="")
colnames(obj) <- paste("CC",1:ncol(obj),sep="")
from <- letters[1:n1]
to <- LETTERS[1:n1]
# to <- 1:n1
res <- recode( obj, from, to )
Print(obj,res,from,to)
res2 <- recode( obj, from, to, useNA=1 )
Print(obj,res2,from,to)


n=10
n1=n-2
obj=cbind( 1:n, n:1 )
rownames(obj)=paste("RR",1:n,sep="")
colnames(obj)=paste("CC",1:ncol(obj),sep="")
from=1:n1
to=from^2
res=recode( obj, from, to, method=2 )
Print(obj,res, from, to)


n <- 10
n1 <- n-2
obj <- cbind( 1:n, n:1 )        # This is numeric.
rownames(obj) <- paste("RR",1:n,sep="")
colnames(obj) <- paste("CC",1:ncol(obj),sep="")
from <- as.character(1:n1)      # This is character.
to <- LETTERS[1:n1]
res <- recode( obj, from, to )
Print(obj,res, from, to)

}
