Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zeros in numbers *within a data frame*

Tags:

dataframe

r

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 sites describing how to drop leading zeros in numbers or strings, including vectors. But none of the descriptions I found seem applicable to data frames.

Or the f_num function in the numform package. It treats "[a] vector of numbers (or string equivalents)", but does not seem to solve unwanted leading zeros in a data frame.

I am relatively new to R but understand that I could develop some (in my mind) complex code to drop leading zeros by subsetting vectors from a data frame and then combining those vectors into a full data frame. I would like to avoid that.

Here is a simple data frame:

df <-  structure(list(est = c(0.05, -0.16, -0.02, 0, -0.11, 0.15, -0.26, 
-0.23), low2.5 = c(0.01, -0.2, -0.05, -0.03, -0.2, 0.1, -0.3, 
-0.28), up2.5 = c(0.09, -0.12, 0, 0.04, -0.01, 0.2, -0.22, -0.17
)), row.names = c(NA, 8L), class = "data.frame")

Which gives

df
    est low2.5 up2.5
1  0.05   0.01  0.09
2 -0.16  -0.20 -0.12
3 -0.02  -0.05  0.00
4  0.00  -0.03  0.04
5 -0.11  -0.20 -0.01
6  0.15   0.10  0.20
7 -0.26  -0.30 -0.22
8 -0.23  -0.28 -0.17

I would want

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

Is that possible with relatively simple code for a whole data frame?


Edit: An incorrect link has been removed.

like image 688
cibr Avatar asked Sep 10 '25 09:09

cibr


1 Answers

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...

like image 180
R Yoda Avatar answered Sep 12 '25 23:09

R Yoda