Remove leading zeros in numbers *within a data frame*

后端 未结 1 757
既然无缘
既然无缘 2021-01-14 07:42

Edit: For anyone coming later: THIS IS NOT A DUPLICATE, since it explicitely concerns work on data frames, not single variables/vectors.


I have found several

相关标签:
1条回答
  • 2021-01-14 07:57

    I am interpreting the intention of your question is to convert each numeric cell in the data.frame into a "pretty-printed" string which is possible using string substitution and a simple regular expression (a good question BTW since I do not know any method to configure the output of numeric data to suppress leading zeros without converting the numeric data into a string!):

    df2 <- data.frame(lapply(df,
                             function(x) gsub("^0\\.", "\\.", gsub("^-0\\.", "-\\.", as.character(x)))),
                      stringsAsFactors = FALSE)
    df2
    #    est low2.5 up2.5
    # 1  .05    .01   .09
    # 2 -.16    -.2  -.12
    # 3 -.02   -.05     0
    # 4    0   -.03   .04
    # 5 -.11    -.2  -.01
    # 6  .15     .1    .2
    # 7 -.26    -.3  -.22
    # 8 -.23   -.28  -.17
    
    str(df2)
    # 'data.frame': 8 obs. of  3 variables:
    # $ est   : chr  ".05" "-.16" "-.02" "0" ...
    # $ low2.5: chr  ".01" "-.2" "-.05" "-.03" ...
    # $ up2.5 : chr  ".09" "-.12" "0" ".04" ...
    

    If you want to get a fixed number of digits after the decimal point (as shown in the expected output but not asked for explicitly) you could use sprintf or format:

    df3 <- data.frame(lapply(df, function(x) gsub("^0\\.", "\\.", gsub("^-0\\.", "-\\.", sprintf("%.2f", x)))), stringsAsFactors = FALSE)
    df3
    #    est low2.5 up2.5
    # 1  .05    .01   .09
    # 2 -.16   -.20  -.12
    # 3 -.02   -.05   .00
    # 4  .00   -.03   .04
    # 5 -.11   -.20  -.01
    # 6  .15    .10   .20
    # 7 -.26   -.30  -.22
    # 8 -.23   -.28  -.17
    

    Note: This solution is not robust against different decimal point character (different locales) - it always expects a decimal point...

    0 讨论(0)
提交回复
热议问题