new_name {lazy.tools}R Documentation

Rename the elements of the input by the new set of names given in the table n2o or by the consecutive numbers.

Description

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

old

An object whose elements are to be replaced by numbers.

n2o

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

sort

= 1 to sort the new name when n2o is NULL.
= 2 to sort the new name when n2o is NULL by naturalsort package.

print

= 1 to print the conversion summary.

Details

By default, the unique names in old will be renamed by the consecutive numbers in the order of appearance.
When old is a data frame, new may be a matrix.

naturalsort package is required to use sort=2 option.

Value

A list of the following:
new renamed version of old: same shape and same order as old
n2o conversion table from new to old
o2n sorted version of n2o by old
freqnew frequency of new names
cbind( n2o,freqnew) will give you the frequency table.
od order to be used to sort an object

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]




[Package lazy.tools version 0.1.4 ]