% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/new_name.R
\name{new_name}
\alias{new_name}
\title{Rename the elements of the input by the new set of names
given in the table n2o or by the consecutive numbers.}
\usage{
new_name(old, n2o = NULL, sort = 0, print = 0)
}
\arguments{
\item{old}{An object whose elements are to be replaced by numbers.}

\item{n2o}{Conversion table (data frame) from old to new or NULL. \cr
Must have two columns named "new" and "old" and "new" must be 1:nrow(n2o).}

\item{sort}{= 1 to sort the new name when n2o is NULL. \cr
= 2 to sort  the new name when n2o is NULL by naturalsort package.}

\item{print}{= 1 to print the conversion summary.}
}
\value{
A list of the following: \cr
  new     renamed version of old:  same shape and same order as old \cr
  n2o     conversion table from new to old \cr
  o2n     sorted version of n2o by old \cr
  freqnew frequency of new names \cr
          cbind( n2o,freqnew) will give you the frequency table. \cr
  od      order to be used to sort an object
}
\description{
Rename the elements of the input by the new set of names
given in the table n2o or by the consecutive numbers.
}
\details{
By default, the unique names in old will be renamed by the
consecutive numbers in the order of appearance. \cr
When old is a data frame, new may be a matrix.
\cr\cr
naturalsort package is required to use sort=2 option.
\cr\cr
}
\examples{
# add consecutive numbers in the order of appearance.
( new_name( matrix(c("IA","NY","WY","AR","NY","IA"),3) ) )

# renaming old
seed <- 1701; set.seed(seed)
old <- sample( letters[1:6], 10, replace=TRUE )
# order of appearance
res1 <- new_name( old, print=1 )
# sorted order
res2 <- new_name( old, print=1, sort=1)
# using renaming table
old2 <- sample( old, 10, replace=TRUE )
res <- new_name( old2, res2$n2o, print=1 )

# Rename "a" as "Z", "b" as "Y", "c" as "X".
new <- c( "Z", "Y", "X" )
old <- c( "a", "b", "c" )
n2o <- data.frame( new=new, old=old, stringsAsFactors=0 )
temp <- new_name( old, n2o )
Print( old, temp$new )
old2 <- sample( old,10,replace=1 )
temp2 <- new_name( old2, n2o )
Print( old2, temp2$new  )

# sorting vectors
# Suppose that you hava a vector
#   vec = c( "A", "D", "B", "E", "C", "D", "A" )
# and you wish to sort it as
#  vec_sorted <- vec[od,]
# so that
#  vec_sorted = c("B", "D", "D", "A", "A", "C", "E") .
# This can be accomplished by using order2 function as:
# vec <- c( "A", "D", "B", "E", "C", "D", "A" )
# od <- order2( vec, order=c("B", "D", "A", "C", "E") )
# vec_sorted <- vec[od]
#
# The same result can be obtained as:#'
vec <- c( "A", "D", "B", "E", "C", "D", "A" )
n2o=data.frame(new=1:5, old=c("B", "D", "A", "C", "E"))
od <- new_name( vec, n2o )$od
vec_sorted <- vec[od]



}
