# Convert Decimals to Reduced Fractions in R (2 Examples)

In this tutorial, Iâ€™ll explain how to convert decimals to reduced fractions in R.

The table of content is structured as follows:

Letâ€™s dive into it!

## Reduced Fractions Explained

Reduced fractions refer to the fractions written in their lowest terms. This implies that the fractionâ€™s numerator and denominator do not have a common factor. For instance, 0.12 can be expressed in fractions in several ways, such as 3/25, 6/50, etc. However, 3/25 is the only reduced fraction among the options.

## Example 1: Decimals to Reduced Fractions (The MASS Package)

For various uses, the entries in decimals (also called decimal numeral; decimal number) can be converted to reduced fractions. The fractions() function of the MASS package is helpful for this implementation.

Letâ€™s illustrate this with an example! A matrix with decimal entries is created as follows for the demonstration.

m<-matrix(1:9/15, 3, 3) m # [,1] [,2] [,3] # [1,] 0.06666667 0.2666667 0.4666667 # [2,] 0.13333333 0.3333333 0.5333333 # [3,] 0.20000000 0.4000000 0.6000000

See below how the decimals are expressed in reduced fractions.

install.packages("MASS") library(MASS) f_mass<-fractions(m) f_mass # [,1] [,2] [,3] # [1,] 1/15 4/15 7/15 # [2,] 2/15 1/3 8/15 # [3,] 1/5 2/5 3/5

### Mathematical Operations

The arithmetic and matrix operations can be applied to reduced fractionsâ€™ output.

sum_f_mass<-f_mass+2 sum_f_mass # [,1] [,2] [,3] # [1,] 2/15 8/15 14/15 # [2,] 4/15 2/3 16/15 # [3,] 2/5 4/5 6/5 trans_f_mass<-t(f_mass) trans_f_mass # [,1] [,2] [,3] # [1,] 1/15 2/15 1/5 # [2,] 4/15 1/3 2/5 # [3,] 7/15 8/15 3/5

However, some operations like matrix multiplication and square root operation may result in decimals.

mult_f_mass<-f_mass%*%f_mass mult_f_mass # [,1] [,2] [,3] # [1,] 0.1333333 0.2933333 0.4533333 # [2,] 0.1600000 0.3600000 0.5600000 # [3,] 0.1866667 0.4266667 0.6666667

The fractions() function has to be called back in such a case.

fractions(mult_f_mass) # [,1] [,2] [,3] # [1,] 2/15 22/75 34/75 # [2,] 4/25 9/25 14/25 # [3,] 14/75 32/75 2/3

## Example 2: Decimals to Reduced Fractions (The Fractional Package)

Alternatively, the same implementation can be conducted via the fractional() function of the fractional package. The mathematical operations are applicable like in the previous case.

install.packages("fractional") library(fractional) f_frac<-fractional(m) f_frac # [,1] [,2] [,3] # [1,] 1/15 4/15 7/15 # [2,] 2/15 1/3 8/15 # [3,] 1/5 2/5 3/5 sqr_f_frac<-f_frac**2 sqr_f_frac # [,1] [,2] [,3] # [1,] 1/225 16/225 49/225 # [2,] 4/225 1/9 64/225 # [3,] 1/25 4/25 9/25

Furthermore, the numerators and denominators can be retrieved separately via the packageâ€™s numerators() and denominators() functions.

numerators(f_frac) # [,1] [,2] [,3] # [1,] 1 4 7 # [2,] 2 1 8 # [3,] 1 2 3 denominators(f_frac) # [,1] [,2] [,3] # [1,] 15 15 15 # [2,] 15 3 15 # [3,] 5 5 5

If you would like to learn more about how to extract numerators and denominators.

The package also offers the unfractional() function which transforms the fractions into decimals.

f_fracback<-unfractional(m) f_fracback # [,1] [,2] [,3] # [1,] 0.06666667 0.2666667 0.4666667 # [2,] 0.13333333 0.3333333 0.5333333 # # [3,] 0.20000000 0.4000000 0.6000000

## Video, Further Resources & Summary

Do you need more explanations on this topic? Then you might check out the following video of the Statistics Globe YouTube channel.

*The YouTube video will be added soon.*

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

- Change Number of Decimal Places on Axis Tick Labels of Plot in R
- Format Number of Decimal Places in R
- Introduction to the R Programming Language

This article has demonstrated how to **convert decimal numerals to reduced fractions in the R programming language**. If you have further questions, you may leave a comment below.

This page was created in collaboration with Cansu Kebabci. You might have a look at Cansuâ€™s author page to get more information about her academic background and the other articles she has written for Statistics Globe.