Learn R Programming

sfsmisc (version 1.1-19)

n.code: Convert "Round" Integers to Short Strings and Back

Description

n.code convert “round integers” to short character strings. This is useful to build up variable names in simulations, e.g.

code2n is the inverse function of n.code().

Usage

n.code(n,    ndig = 1, dec.codes = c("", "d", "c", "k"))
code2n(ncod, ndig = 1, dec.codes = c("", "d", "c", "k"))

Value

n.code(n) returns a character vector of the same length as n.

code2n(ncod) returns a integer vector of the same length as ncod.

Usually, code2n(n.code(n)) == n.

Arguments

n

integer vector.

ncod

character vector, typically resulting from n.code.

ndig

integer giving number of digits before the coding character.

dec.codes

character code for 1, 10, 100, 1000 (etc).

Author

Martin Maechler

Examples

Run this code
n10 <-  c(10,20,90, 100,500, 2000,10000)
(c10 <- n.code(n10))#-> "1d" "2d" "9d" "1c" ..
stopifnot(code2n(c10) == n10)

Run the code above in your browser using DataLab