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 thanks
Hasan Diwan
2015-Feb-26 02:26 UTC
[R] How many digits are there in left of dot of 0.0001 ?
On 25 February 2015 at 17:55, ce <zadig_1 at excite.com> wrote:> Dear all, > > I would like to count how many digits are there on the left of a the dot > of a numeric variable >Left? An infinite number... What does this have to do with R, though? -- H> > a=0.0001 > > thanks > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- OpenPGP: https://hasan.d8u.us/gpg.key Sent from my mobile device Envoy? de mon portable [[alternative HTML version deleted]]
Hi, I assume you want to know the digit count to the left of decimal point. If this is the case, then you may use trunc(log10(max(1,trunc(abs(a)))))+1 for a numerical variable a. Count 0.12 as having one digit to the left of decimal point.> trunc(log10(max(1,trunc(abs(-100000.99)))))+1[1] 6> trunc(log10(max(1,trunc(abs(0)))))+1[1] 1> trunc(log10(max(1,trunc(abs(9.999)))))+1[1] 1> trunc(log10(max(1,trunc(abs(19.999)))))+1[1] 2> trunc(log10(max(1,trunc(abs(-1999.999)))))+1[1] 4 -- View this message in context: http://r.789695.n4.nabble.com/How-many-digits-are-there-in-left-of-dot-of-0-0001-tp4703842p4703847.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2015-Feb-26 12:17 UTC
[R] How many digits are there in left of dot of 0.0001 ?
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.0001This 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