[R] cutting out numbers from vectors

Paul Roebuck plroebuck at mdanderson.org
Fri Aug 1 06:49:36 CEST 2008


On Thu, 31 Jul 2008, calundergrad wrote:

> i have a vector with values similar to the below text
> [1] 001-010-001-0
>
> I want to get rid of all leading zeroes.
> for example i want to change the values of the vector
> so that [1] 001-010-001-0 becomes [1] 1-010-001-0.
>
> Another example
> [1]082-232-232-1 becomes [1] 82-232-232-1
>

xform <- function(nstr) {
    nstr.vec <- unlist(strsplit(nstr, '-'))
    nstr.vec[1] <- as.character(as.integer(nstr.vec[1]))
    return(paste(nstr.vec, collapse='-'))
}

stopifnot(xform('001-010-001-0') == '1-010-001-0')
stopifnot(xform('082-232-232-1') == '82-232-232-1')

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)



More information about the R-help mailing list