[R] Truncating leading zeros in strings

E. Paul Wileyto epw at mail.med.upenn.edu
Thu Oct 7 16:46:08 CEST 2010


  Thanks...  As I mentioned, I am new to R, and "as.integer" was not 
what I stumbled onto.  I looked up string conversion in the reference 
manual, and "strtoi" is what came up.

P

On 10/7/2010 10:35 AM, David Winsemius wrote:
>
> 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.

-- 
E. Paul Wileyto, Ph.D.
Assistant Professor of Biostatistics
Tobacco Use Research Center
School of Medicine, U. of Pennsylvania
3535 Market Street, Suite 4100
Philadelphia, PA  19104-3309

215-746-7147
Fax: 215-746-7140
epw at upenn.edu



More information about the R-help mailing list