# Automatic determination of the number of significant figures to retain

November 17, 2012

In exploratory analysis, I often want to retain just enough significant figures so that each datum of a continuous variable is unique. I got sick of trial and error so I wrote an R function for this:

```
round.unique <- function(x, digits = 0:10){
for(i in digits){
xout <- round(x, i)
if(!anyDuplicated(xout)) return(xout)
}
stop(paste('More than', max(digits), 'digits to required for uniqueness'))
}
```

Advertisements

One Comment
leave one →

I like it, seems very useful.

R drives me crazy with trying to figure out how to format numbers properly (among other things). Usually I just give up on it.