[R] how to calculate a numeric's digits count?

Olivier Crouzet olivier.crouzet at univ-nantes.fr
Fri Oct 24 09:05:57 CEST 2014


Le Fri, 24 Oct 2014 14:11:08 +0800 (CST), PO SU a écrit :

> 
> Ok,  what i want is  find how many numbers after  . in a numeric ,and
> i don't know if there is already exists a function to do it( i wrote
> one by myself which will be showed later). e.g. 1.234 has 3 numbers
> after . 1 has 0 number
> 1.5342 has 4 numbers 
> And i solved the above format using:
> find<-function(x)
> {
>     str<-as.character(x)
>     if(is.na(strsplit(str,"\\.")[[1]][2])) return(0)
      else return(nchar(strsplit(str,"\\.")[[1]][2]))  
> }

It appears that your initial vector (x) is a numeric one that you
(obviously) need to transform to a character vector. So any number
it contains will have been shortened to its minimal representation when
you convert it to string. As far as I can tell (though I'm no expert in
number representation), you should work with x as a string vector from
the very beginning (which, to me, seems rather intuitive as the 0 in
1.230 is, really, only a string isn't it ?)

If you can "see" zeros when you print the numeric vector by e.g. 
print (x), that's simply because R displays them with a default
precision but they may contain many more digits... Try dput (x) before
str <- as.character (x) and you will see what I mean.

Also, it may be usefull to look at what the following lines produce :
sprintf("%.10f", x)
sprintf("%.30f", x)

You will see that actually R stores many more digits that what you
think there are and that your "0" in "1.340" for example is,
really not single... except when it is basically a string.

Olivier.

> 
> But when i  find(1.340)  i get 2 not 3. find(1.3400) will also get 2
> not 4. So,my question is how to implement the above needing? TKS.
> 
> 
> 
> 
> --
> 
> PO SU
> mail: desolator88 at 163.com 
> Majored in Statistics from SJTU
> 
> 
> 
> 
> At 2014-10-24 12:04:18, "Jeff Newmiller" <jdnewmil at dcn.davis.CA.us>
> wrote:
> >I am baffled. I think those were English words but they didn't make
> >any sense to me. Not was there a reproducible example to turn to.
> >Can you try again?
> >---------------------------------------------------------------------------
> >Jeff Newmiller                        The     .....       .....  Go
> >Live... DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.
> >##.#.  Live Go...
> >                                      Live:   OO#.. Dead: OO#..
> > Playing
> >Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> >/Software/Embedded Controllers)               .OO#.       .OO#.
> >rocks...1k
> >--------------------------------------------------------------------------- 
> >Sent from my phone. Please excuse my brevity.
> >
> >On October 23, 2014 8:35:06 PM PDT, PO SU <rhelpmaillist at 163.com>
> >wrote:
> >>
> >>Dear usRers,
> >>  Now i want to cal ,e.g. 
> >> cal(1.234)  will get 3
> >> cal(1) will get 0
> >> cal(1.3045) will get 4
> >> But the difficult part is cal(1.3450) will get 4 not 3.
> >>So, is there anyone happen to know the solution to this problem, or
> >>it can't be solved in R, because 1.340 will always be transformed
> >>autolly to 1.34?
> >>
> >>
> >>
> >>
> >>
> >>
> >>--
> >>
> >>PO SU
> >>mail: desolator88 at 163.com 
> >>Majored in Statistics from SJTU
> >>______________________________________________
> >>R-help at r-project.org mailing list
> >>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.
> >
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list