[R] Leading zeros

jim holtman jholtman at gmail.com
Fri Aug 19 21:15:50 CEST 2011


Here is yet another way of prepending leading zeros on a string:

> x <- c('123/1234', '234/12', '21342342134/34', '99')
> n <- 10  # upto 10 leading zeros (max length of string)
> leading <- paste(rep('0', n), collapse = '')
> # add up to 10 zeros and then truncate to max length, but at least keep original
> substring(paste(leading, x, sep = '')
+            , pmin(11, nchar(x) + 1)  # starting location
+            , n + nchar(x)  # ending location
+            )
[1] "00123/1234"     "0000234/12"     "21342342134/34" "0000000099"

On Fri, Aug 19, 2011 at 12:41 PM, Ole Peter Smith <ole.ufg at gmail.com> wrote:
> ---------- Forwarded message ----------
> From: Ole Peter Smith <ole.ufg at gmail.com>
> Date: Fri, Aug 19, 2011 at 1:40 PM
> Subject: Re: [R] Leading zeros
> To: David Winsemius <dwinsemius at comcast.net>
>
>
> I'm all new to R, assisting the last days of topics from the sideline.
> I am, however, a longterm programmer.
>
> This question makes me ask myself - and now here - is there any string
> split-function, ex some thing like:
>
> split('/',"2000/3000/4") --> (2000,3000,4)
>
> After this patting with zero is a breeze, sprintf('%010d',...
>
> 0le
>
> On Fri, Aug 19, 2011 at 1:19 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>> Copying list one what was sent in reply. Anybody have a better solution?
>>
>>> On Aug 19, 2011, at 11:57 AM, Vasco Cadavez wrote:
>>>
>>>> Thanks,
>>>>
>>>> A solution can be by substring to remove the /
>>>> then numeric will be ok! What you think?
>>>>
>>>> How can I remove the /
>>>
>> with sub or gsub:
>>
>>> sprintf("%010.0f", as.integer(gsub("/","", c("4/3003","55/333","66/22"))
>>> ))
>> [1] "0000043003" "0000055333" "0000006622"
>>
>> --
>> David.
>>
>>>>
>>>> Thanks
>>>>
>>>> Vasco Cadavez
>>>>
>>>> ----- Menssagem Original -----
>>>> De:
>>>> "David Winsemius" <dwinsemius at comcast.net>
>>>>
>>>> Para:
>>>> "David Winsemius" <dwinsemius at comcast.net>
>>>> Cópia:
>>>> "Vasco Cadavez" <vcadavez at ipbpt>, <r-help at r-project.org>
>>>> Enviado:
>>>> Fri, 19 Aug 2011 11:51:08 -0400
>>>> Assunto:
>>>> Re: [R] Leading zeros
>>>>
>>>>
>>>>
>>>> On Aug 19, 2011, at 11:17 AM, David Winsemius wrote:
>>>>
>>>> >
>>>> > On Aug 19, 2011, at 11:12 AM, Vasco Cadavez wrote:
>>>> >
>>>> >> Hello,
>>>> >> I have a dataset with an Id columns like:
>>>> >> 4/3003
>>>> >> 55/333
>>>> >> 66/22
>>>> >> I want to put leading zeros to get:
>>>> >> 00000004/3003
>>>> >> 000000055/333
>>>> >> 0000000066/22
>>>> >>
>>>> >> How can I solve this?
>>>> >
>>>> > ?sprintf
>>>> > ?formatC
>>>> >
>>>> I may have been too quick. Padding with leading zeros using sprintf is
>>>> described for numeric but not for character types. There are severa
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
>                                             /////
>                                          ( O O )
> =================oOO==(_)==OOo=================
>       God does not care about our mathematical difficulties.
>                     He integrates empirically - Einstein
>                                 .oooO   Oooo.
> ==================(     )=(     )=====================
>                                      \  (     )  /
>                                       \_)   (_/
> ===============================================
>                         Ole Peter Smith, IME, UFG
>     http://www.mat.ufg.br/docentes/olepeter - ole at mat.ufg.br
> ===============================================
>                    Life sure is a Mystery to be Lived
>                       Not a Problem to be Solved
> ===============================================
>
>
>
> --
>                                             /////
>                                          ( O O )
> =================oOO==(_)==OOo=================
>       God does not care about our mathematical difficulties.
>                     He integrates empirically - Einstein
>                                 .oooO   Oooo.
> ==================(     )=(     )=====================
>                                      \  (     )  /
>                                       \_)   (_/
> ===============================================
>                         Ole Peter Smith, IME, UFG
>     http://www.mat.ufg.br/docentes/olepeter - ole at mat.ufg.br
> ===============================================
>                    Life sure is a Mystery to be Lived
>                       Not a Problem to be Solved
> ===============================================
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list