[R] Truncating leading zeros in strings

Martyn Byng Martyn.Byng at nag.co.uk
Thu Oct 7 16:36:13 CEST 2010


Hi,

Think I might be misunderstanding what you want, because ...

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


anyway

> t = "0000123"
> substr(t,regexpr("[^0]",t),nchar(t))
[1] "123"

seems to trim the leading 0's

Martyn

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of E. Paul Wileyto
Sent: 07 October 2010 14:58
To: r-help at r-project.org
Subject: [R] Truncating leading zeros in strings


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?

Thanks

Paul






-- 
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

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}



More information about the R-help mailing list