2

Is there any way of stopping R from dropping leading zeros in an integer? e.g.,

a<-c(00217,00007,00017)

I understand this is not the correct way of writing integers. Sadly I've been given a text file (person and non-R code are not around anymore) containing thousands of vectors in a single list: list(drugA=c(...), drugB=c(....),........)

I need to keep the leading zeros as 00002 becomes 2. I could load these thousands of values in and then write a function to parse the list and convert into characters whilst correcting for any number that isn't five characters long but I was hoping for a speedy alternative.

UPDATE1 An example of the text file I've been provided:

list(CETUXIMAB=c(05142,05316),
DORNASEALFA=c(94074),
ETANERCEPT=c(05342,99075),
BIVALIRUDIN=c(04400,09177),
LEUPROLIDE=c(02074,03219,91035,91086),
PEGINTERFERONALFA2A=c(03162),
ALTEPLASE=c(00486,01032,03371,05314),
DARBEPOETINALFA=c(02217,03421),
GOSERELIN=c(99221),
RETEPLASE=c(00157),
ERYTHROPOIETIN=c(92078,92122)) 

I have truncated the list as there are thousands of vectors. This was a text file generated using a program written in C++ (code not available). Some of the values e.g., RETEPLASE=c(00157) becomes truncated to 157.

Anthony Nash
  • 834
  • 1
  • 9
  • 26
  • Anthony, can you provide an example of the data you are working with. Separately, my sense is to recommend you review the following question and answer and linked answers. I hope this points you in the right direction. https://stackoverflow.com/questions/5812493/adding-leading-zeros-using-r?rq=1 and https://stackoverflow.com/questions/linked/5812493?lq=1 I hope the above helps. – Technophobe01 Nov 04 '17 at 18:41
  • 1
    That data is not numeric data. Import it as characters. – Roland Nov 04 '17 at 18:47
  • Thanks for those links, Technophobe01. I've added a larger real example. – Anthony Nash Nov 04 '17 at 18:55
  • Also, `sprintf("%05d", 2)`. – Rui Barradas Nov 04 '17 at 19:22
  • Use text processing to add quotes around the numbers before you source the file. Obviously, the person who created this file had no clue what they were doing. – Roland Nov 04 '17 at 19:24

1 Answers1

4
library(stringr)
str_pad(a, 5, pad = "0")
B Williams
  • 1,992
  • 12
  • 19