Dear all,
Are there any functions to convert decimals to fractions in R?
I have the result:
> summary(as.factor(complete.ID))
0 0.0133333333333333 0.04
2256 488 230
0.0666666666666667 0.0933333333333333 0.106666666666667
2342 310 726
0.133333333333333 0.146666666666667 0.16
179 750 163
0.186666666666667 0.2 0.226666666666667
194 180 57
0.253333333333333 0.266666666666667 0.293333333333333
1 10 40
0.32 0.333333333333333 0.346666666666667
45 61 117
0.373333333333333
62
>
How to convert something like this:
0 1/75 3/75
2256 488 230
5/75 7/75 8/75
2342 310 726
10/75 11/75 12/75
179 750 163
14/75 15/75 17/75
194 180 57
19/75 20/75 22/75
1 10 40
24/75 25/75 26/75
45 61 117
28/75
62
Any suggestions would be appreciated. Thanks you.
Best, Muhammad Subianto
PS.
I found this website: http://www.mindspring.com/~alanh/fracs.html
G'day Muhammad,>>>>> "MS" == Muhammad Subianto <msubianto at gmail.com> writes:MS> Are there any functions to convert decimals to fractions in R? MS> I have the result: Something like:> library(MASS) > as.fractions(c(0, 0.0133333333333333, 0.04,0.0666666666666667, 0.0933333333333333, 0.106666666666667, 0.133333333333333, 0.146666666666667, 0.16, 0.186666666666667, 0.2, 0.226666666666667, 0.253333333333333, 0.266666666666667, 0.293333333333333, 0.32, 0.333333333333333, 0.346666666666667, 0.373333333333333)) [1] 0 1/75 1/25 1/15 7/75 8/75 2/15 11/75 4/25 14/75 1/5 17/75 [13] 19/75 4/15 22/75 8/25 1/3 26/75 28/75 ? Cheers, Berwin
library(MASS)
?fractions
help.search("fractions") gets you there.
On Fri, 27 Jan 2006, Muhammad Subianto wrote:
> Dear all,
> Are there any functions to convert decimals to fractions in R?
> I have the result:
> > summary(as.factor(complete.ID))
> 0 0.0133333333333333 0.04
> 2256 488 230
> 0.0666666666666667 0.0933333333333333 0.106666666666667
> 2342 310 726
> 0.133333333333333 0.146666666666667 0.16
> 179 750 163
> 0.186666666666667 0.2 0.226666666666667
> 194 180 57
> 0.253333333333333 0.266666666666667 0.293333333333333
> 1 10 40
> 0.32 0.333333333333333 0.346666666666667
> 45 61 117
> 0.373333333333333
> 62
> >
>
> How to convert something like this:
>
> 0 1/75 3/75
> 2256 488 230
> 5/75 7/75 8/75
> 2342 310 726
> 10/75 11/75 12/75
> 179 750 163
> 14/75 15/75 17/75
> 194 180 57
> 19/75 20/75 22/75
> 1 10 40
> 24/75 25/75 26/75
> 45 61 117
> 28/75
> 62
>
> Any suggestions would be appreciated. Thanks you.
> Best, Muhammad Subianto
> PS.
> I found this website: http://www.mindspring.com/~alanh/fracs.html
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
if it happens to know the denominator, then a simple approach could
be:
frac.fun <- function(x, den){
dec <- seq(0, den) / den
nams <- paste(seq(0, den), den, sep = "/")
sapply(x, function(y) nams[which.min(abs(y - dec))])
}
#######################
frac.fun(c(0, 1, 0.8266667, .066666, 0.2666666), 75)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Muhammad Subianto" <msubianto at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Friday, January 27, 2006 11:39 AM
Subject: [R] How to convert decimals to fractions
> Dear all,
> Are there any functions to convert decimals to fractions in R?
> I have the result:
> > summary(as.factor(complete.ID))
> 0 0.0133333333333333 0.04
> 2256 488 230
> 0.0666666666666667 0.0933333333333333 0.106666666666667
> 2342 310 726
> 0.133333333333333 0.146666666666667 0.16
> 179 750 163
> 0.186666666666667 0.2 0.226666666666667
> 194 180 57
> 0.253333333333333 0.266666666666667 0.293333333333333
> 1 10 40
> 0.32 0.333333333333333 0.346666666666667
> 45 61 117
> 0.373333333333333
> 62
> >
>
> How to convert something like this:
>
> 0 1/75 3/75
> 2256 488 230
> 5/75 7/75 8/75
> 2342 310 726
> 10/75 11/75 12/75
> 179 750 163
> 14/75 15/75 17/75
> 194 180 57
> 19/75 20/75 22/75
> 1 10 40
> 24/75 25/75 26/75
> 45 61 117
> 28/75
> 62
>
> Any suggestions would be appreciated. Thanks you.
> Best, Muhammad Subianto
> PS.
> I found this website: http://www.mindspring.com/~alanh/fracs.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
On this day 27/01/2006 11:51, Berwin A Turlach wrote: >> library(MASS) >> as.fractions(c(0, 0.0133333333333333, 0.04, > 0.0666666666666667, 0.0933333333333333, 0.106666666666667, > 0.133333333333333, 0.146666666666667, 0.16, > 0.186666666666667, 0.2, 0.226666666666667, > 0.253333333333333, 0.266666666666667, 0.293333333333333, > 0.32, 0.333333333333333, 0.346666666666667, > 0.373333333333333)) > > [1] 0 1/75 1/25 1/15 7/75 8/75 2/15 11/75 4/25 14/75 1/5 17/75 > [13] 19/75 4/15 22/75 8/25 1/3 26/75 28/75 > On this day 27/01/2006 11:53, Prof Brian Ripley wrote:> library(MASS) > ?fractions > > help.search("fractions") gets you there. >Many Thanks to Berwin A Turlach and Prof Brian Ripley for your suggestions. > ?fractions > Best regards, Muhammad Subianto
Thanks you for your help. Best wishes, Muhammad Subianto On this day 27/01/2006 12:08, Dimitris Rizopoulos wrote:> if it happens to know the denominator, then a simple approach could > be: > > frac.fun <- function(x, den){ > dec <- seq(0, den) / den > nams <- paste(seq(0, den), den, sep = "/") > sapply(x, function(y) nams[which.min(abs(y - dec))]) > } > ####################### > frac.fun(c(0, 1, 0.8266667, .066666, 0.2666666), 75) > > > I hope it helps. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/(0)16/336899 > Fax: +32/(0)16/337015 > Web: http://www.med.kuleuven.be/biostat/ > http://www.student.kuleuven.be/~m0390867/dimitris.htm > > > ----- Original Message ----- > From: "Muhammad Subianto" <msubianto at gmail.com> > To: <R-help at stat.math.ethz.ch> > Sent: Friday, January 27, 2006 11:39 AM > Subject: [R] How to convert decimals to fractions > > >> Dear all, >> Are there any functions to convert decimals to fractions in R? >> I have the result: >>> summary(as.factor(complete.ID)) >> 0 0.0133333333333333 0.04 >> 2256 488 230 >> 0.0666666666666667 0.0933333333333333 0.106666666666667 >> 2342 310 726 >> 0.133333333333333 0.146666666666667 0.16 >> 179 750 163 >> 0.186666666666667 0.2 0.226666666666667 >> 194 180 57 >> 0.253333333333333 0.266666666666667 0.293333333333333 >> 1 10 40 >> 0.32 0.333333333333333 0.346666666666667 >> 45 61 117 >> 0.373333333333333 >> 62 >> How to convert something like this: >> >> 0 1/75 3/75 >> 2256 488 230 >> 5/75 7/75 8/75 >> 2342 310 726 >> 10/75 11/75 12/75 >> 179 750 163 >> 14/75 15/75 17/75 >> 194 180 57 >> 19/75 20/75 22/75 >> 1 10 40 >> 24/75 25/75 26/75 >> 45 61 117 >> 28/75 >> 62 >> >> Any suggestions would be appreciated. Thanks you. >> Best, Muhammad Subianto >> PS. >> I found this website: http://www.mindspring.com/~alanh/fracs.html >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html