Hi everybody! I'm new to this list and also to the R program. I'd like to know if there is a function able to convert results into Fractional form like my scientific calculator have. For example:> 1/3[1] 0.3333333> function_that_return_a_fraction_from_numbers(0.3333333)[1] 1/3 Thanks Mauro -- Man, he is constantly growing and when he is bound by a set pattern of ideas or way of doing things, that's when he stops growing -- Bruce Lee
Mauro Arnoldi wrote:> Hi everybody! > I'm new to this list and also to the R program. > > I'd like to know if there is a function able to convert results into > Fractional form like my scientific calculator have. For example: > >> 1/3 > [1] 0.3333333 > >> function_that_return_a_fraction_from_numbers(0.3333333) > [1] 1/30.333333 is not equal 1/3, hence it is impossible to do that in a reliable manner. Uwe Ligges> Thanks > > Mauro >
Welcome (benvenuto), see "fractions" in library MASS es:> fractions(1/2)[1] 1/2 Ciao Stefano On Thu, Sep 13, 2007 at 04:54:01PM +0200, Mauro Arnoldi wrote: <Mauro>Hi everybody! <Mauro>I'm new to this list and also to the R program. <Mauro> <Mauro>I'd like to know if there is a function able to convert results into <Mauro>Fractional form like my scientific calculator have. For example: <Mauro> <Mauro>> 1/3 <Mauro>[1] 0.3333333 <Mauro> <Mauro>> function_that_return_a_fraction_from_numbers(0.3333333) <Mauro>[1] 1/3 <Mauro> <Mauro>Thanks <Mauro> <Mauro>Mauro <Mauro> <Mauro>-- <Mauro> <Mauro> <Mauro>Man, he is constantly growing <Mauro>and when he is bound by a set <Mauro>pattern of ideas or way <Mauro>of doing things, that's when <Mauro>he stops growing <Mauro> -- Bruce Lee <Mauro> <Mauro>______________________________________________ <Mauro>R-help a r-project.org mailing list <Mauro>https://stat.ethz.ch/mailman/listinfo/r-help <Mauro>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <Mauro>and provide commented, minimal, self-contained, reproducible code.
Try: library(MASS) ?fractions On 9/13/07, Mauro Arnoldi <agharti81 at tiscali.it> wrote:> Hi everybody! > I'm new to this list and also to the R program. > > I'd like to know if there is a function able to convert results into > Fractional form like my scientific calculator have. For example: > > > 1/3 > [1] 0.3333333 > > > function_that_return_a_fraction_from_numbers(0.3333333) > [1] 1/3 > > Thanks > > Mauro > > -- > > > Man, he is constantly growing > and when he is bound by a set > pattern of ideas or way > of doing things, that's when > he stops growing > -- Bruce Lee > > ______________________________________________ > 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. >
look at ?fractions> fractions(.33333)[1] 1/3 On Thu, 13 Sep 2007, Mauro Arnoldi wrote:> Hi everybody! > I'm new to this list and also to the R program. > > I'd like to know if there is a function able to convert results into > Fractional form like my scientific calculator have. For example: > > > 1/3 > [1] 0.3333333 > > > function_that_return_a_fraction_from_numbers(0.3333333) > [1] 1/3 > > Thanks > > Mauro > > -- > > > Man, he is constantly growing > and when he is bound by a set > pattern of ideas or way > of doing things, that's when > he stops growing > -- Bruce Lee > > ______________________________________________ > 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. >
Alle gioved? 13 settembre 2007, Stefano Calza ha scritto:> > see "fractions" in library MASS >Yeah!> 1/3[1] 0.3333333> fractions(0.3333333)[1] 1/3 Great! Thanks! (grazie :) ) Mauro
On Thu, 13 Sep 2007, Mauro Arnoldi wrote:> Hi everybody! > I'm new to this list and also to the R program. > > I'd like to know if there is a function able to convert results into > Fractional form like my scientific calculator have. For example: > >> 1/3 > [1] 0.3333333 > >> function_that_return_a_fraction_from_numbers(0.3333333) > [1] 1/3 >This must have some restrictions (so it doesn't return 3333333/1000000, which would be a more accurate fraction). One approach is> unfrac <- function(x, max=100, tol=0.01){num <- x * (1:max) err <- (num - round(num)) * (1:max) if (!any(abs(err) < tol)) return(NA) i <- which.min(abs(err)) c(round(num[i]), i) } This returns the best fraction approximation with denominator up to `max`, where `best` is in terms of the non-integer part of the numerator, and no answer is given if the non-integer part of the numerator is more than `tol`> unfrac(0.3333333)[1] 1 3> unfrac(pi)[1] NA> unfrac(pi,max=1000)[1] 355 113> unfrac(pi,tol=0.1)[1] 22 7 -thomas
Apparently Analagous Threads
- calculation fraction/ratio
- Rounding fractional numbers to nearest fraction
- how many hosts could be down in a 12x(4+2) distributed dispersed volume?
- "file changed as we read it" message during tar file creation on GlusterFS
- "file changed as we read it" message during tar file creation on GlusterFS