Search code examples
rformattingdecimaltransformationdigits

replace decimal point from strings in entire column


I am working on a data set with columns with numbers like this:

icd9code
285.21
593.9
285.21
v04.81

in order to run the R comorbidities package, I need to change them to 5 digits numbers without decimal points.

so they need to look like this:

icd9code
28521
59390
28521
v0481

What function can I use? In particular, how can I get it to show 0 at the end of the number if it has only 4 digits. Also, how can I transfer number starts with 'v'?


Solution

  • Here's a vectorized solution:

    x <- c("285.21", "593.9", "285.21", "v04.81")
    
    substr(gsub("\\.", "", paste0(x, "00000")), 1, 5)
    # [1] "28521" "59390" "28521" "v0481"