How to convert a decimal value or a vector of decimal values to fractional form in R?

R ProgrammingServer Side ProgrammingProgramming

A fraction form of a decimal value is the form of the value represented with division sign. For example, representing 0.5 as 1 / 2. In R, we can use fractions function of MASS package to convert a decimal value or a vector of decimal values to fractional form. To do so, we just need to pass the value in fractions function as fractions(“Decimal_value or Vector_Of_Decimal_Values”).

Loading MASS package −

Examples

> library(MASS)

Output

> fractions(0.14)
[1] 7/50
> fractions(1.14)
[1] 57/50
> library(MASS)
> fractions(0.5)
[1] 1/2
> fractions(0.3)
[1] 3/10
> fractions(0.31)
[1] 31/100
> fractions(1.31)
[1] 131/100
> fractions(2.01)
[1] 201/100
> fractions(1.01001)
[1] 101001/1e+05
> fractions(4.211)
[1] 4211/1000
> fractions(215.11)
[1] 21511/100
> fractions(215.15)
[1] 4303/20
> fractions(15.77)
[1] 1577/100
> fractions(16.73)
[1] 1673/100
> fractions(100.07)
[1] 10007/100
> fractions(100.17)
[1] 10017/100
> fractions(1.17)
[1] 117/100
> fractions(2.65)
[1] 53/20
> fractions(1.7)
[1] 17/10
> fractions(1.65)
[1] 33/20
> fractions(78.35)
[1] 1567/20
> fractions(43.95)
[1] 879/20
> fractions(0.005)
[1] 1/200
> fractions(0.025)
[1] 1/40
> fractions(0.00625)
[1] 1/160
> fractions(1111.1111)
[1] 11111111/10000
> fractions(c(0.2,0.07,0.75,0.84,1.35,2.65,7.95))
[1] 1/5 7/100 3/4 21/25 27/20 53/20 159/20
> fractions(c(14.2,798.37,12.75,15.84,1.35,2.65,7.95))
[1] 71/5 79837/100 51/4 396/25 27/20 53/20 159/20
raja
Published on 04-Sep-2020 09:31:35
Advertisements