[R] How many digits are there in left of dot of 0.0001 ?

Duncan Murdoch murdoch.duncan at gmail.com
Thu Feb 26 13:17:49 CET 2015


On 25/02/2015 8:55 PM, ce wrote:
> Dear all,
> 
> I would like to count how many digits are there on the left of a the dot of a numeric variable
> 
> a=0.0001

This will depend on the formatting used.  If default formatting used by
as.character() is fine, then

nchar(sub("^[[:digit:]]*[.]", "", a))

should work. (Note that default formatting is scientific for 0.0001.) If
you want some other formatting, then format first, and pass a character
object, e.g.

chars <- format(a, scientific = FALSE)
nchar(sub("^[[:digit:]]*[.]", "", chars))

Duncan Murdoch



More information about the R-help mailing list