Skip to content

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 →
  1. November 18, 2012 5:53 pm

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: