Search code examples
rfractions

Getting numerator and denominator of a fraction in R


Using the function fractions in the library MASS, I can convert a decimal to a fraction:

> fractions(.375)
  [1] 3/8

But then how to I extract the numerator and denominator? The help for fractions mentions an attribute "fracs", but I can't seem to access it.


Solution

  • A character representation of the fraction is stored in an attribute:

    x <- fractions(0.175)
    > strsplit(attr(x,"fracs"),"/")
    [[1]]
    [1] "7"  "40"