[R] How to convert decimals to fractions

Muhammad Subianto msubianto at gmail.com
Fri Jan 27 13:05:16 CET 2006


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




More information about the R-help mailing list