Description Usage Arguments Details Value Methods (by class)
View source: R/fun-insert_zeros.R
Insert a string of zeros into every item of a character vector.
A1
becomes A01
.
The orginal intent was to fix sorting of strings where 2 > 12
but 02 < 12
.
More robust options were built in just in case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | insert_zeros(x, ...)
## S3 method for class 'character'
insert_zeros(x, zeros = "auto", after = 1, ...)
## S3 method for class 'factor'
insert_zeros(x, zeros = "auto", after = 1, ...)
## S3 method for class 'integer'
insert_zeros(x, zeros = "auto", after = 1, ...)
## S3 method for class 'double'
insert_zeros(x, zeros = "auto", after = 1, ...)
## Default S3 method:
insert_zeros(x, zeros = "auto", after = 1, ...)
|
x |
character or integer vector;
double vectors are handled only if they can be converted to integer and pass as |
... |
arguments passed to methods |
zeros |
number of |
after |
position of the character after which the zeros will be inserted |
The zeros
argument can be a fixed number but it defaults to auto
. Then:
if X
is of length 1, adds 1 0
;
if X
is longer than 1, adds to each element a number of 0
so that all elements of X
have an equal number of characters.
Modified character vector, where strings of zeros have been inserted at the specified position.
character
: the basic method
factor
: runs on levels of x rather than its body
integer
: coerces to character and calls character method
double
: attempts to convert to integer and calls integer method
default
: throws error for non-supperted classes
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.