[R] Truncating leading zeros in strings

David Winsemius dwinsemius at comcast.net
Thu Oct 7 16:35:49 CEST 2010


On Oct 7, 2010, at 9:57 AM, E. Paul Wileyto wrote:

>
> I am new to R.  I thing this will be simple, but I don't yet know my  
> way around.
>
> I am generating character strings from the system clock that  
> represent integers, and I want to convert them to integer values.   
> Strtoi works well, except when there are leading zeros on the  
> string.  Could anyone suggest a way to remove those leading zeros?

as.integer doesn't work? (If you are dealing in different base than  
10, then you really should say so.)

 > as.integer("0000123")
[1] 123

Or pass through sub("^[0]+", "", vals)
 > sub("^[0]+", "", "0000123")
[1] "123"

An example is expected in such situations. Please do now read the  
Posting Guide.
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list