Search code examples
rdatatablecalculated-columns

Dividing column in R with both letter and numbers/Removing leading zeros of certain values


I have two problems. I'm trying to remove leading zeros for any number that is larger than 100 using R.

Obs Phecode
1   008 
2   009
3   1000
4   0100

I want it to look like this

Obs Phecode
1   008 
2   009
3   1000
4   100

Also, is there a way to divide a column by a certain number even though some of the rows have letters in them? I also want to have leading zeroes if the value is less than 100, but only if there's not a letter in the beginning.

Original Table

Obs ICD
1   99561 
2   980
3   E440
4   V4489

Desired Table

Obs ICD
1   995.61 
2   009.8
3   E4.40
4   V44.89

Thanks in advance!


Solution

  • If a number is greater than equal to 100, change it to numeric (as.numeric(..) removes leading 0's) else keep the value as it is.

    df <- transform(df, Phecode = ifelse(as.numeric(Phecode) >= 100, 
                                    as.numeric(Phecode), Phecode))
    
    df
    #  Obs Phecode
    #1   1     008
    #2   2     009
    #3   3    1000
    #4   4     100