# Convert Fraction to Decimal in R (2 Examples)

In this tutorial, Iâ€™ll illustrate how to **convert fractions to decimals** in the R programming language.

Table of contents:

Letâ€™s just jump right in!

## Example Data

Have a look at the following example data:

frac_num <- c(3/4, 4/5, 5/6) # create numeric vector of fractions class(frac_num) # print data class of frac_num # "numeric" frac_char <- c("3/4", "4/5", "5/6") # create character vector of fractions class(frac_char) # print data class of frac_char # "character"

I have created two different vectors, one containing the fractions as numerics and the other consisting of fractions as characters.

## Example 1: Transform Numeric Vector of Fractions to Decimals

This example explains how to transform a numeric vector of fractions into a vector of decimals. When the fractions are given in the numeric class, like in `frac_num`

, no attempt is needed to convert them to decimals since R immediately executes the division operation. Letâ€™s see how!

decimals <- frac_num # define decimals decimals # print decimals # [1] 0.7500000 0.8000000 0.8333333

As you can see above, `frac_num`

has been assigned to a new variable called `decimals`

. Then the print of `decimals`

resulted in the corresponding vector of decimals. Needless to say, you would get the same result if you printed `frac_num`

directly.

However, some additional attempts are necessary if the fractional data is recorded as characters. Letâ€™s check it in the next example!

## Example 2: Transform Character Vector of Fractions to Decimals

If you have a character vector of fractions, you need to use multiple functions combined. Below, you can see how the `decimals`

are defined using nested functions.

decimals <- unname(sapply(frac_char, function(x) eval(parse(text = x)))) # evaluate fraction expressions as arithmetic operations decimals # print decimals # [1] 0.7500000 0.8000000 0.8333333

In this example, first, I have parsed the strings into R expressions via parse(), which enables R to evaluate the input as an R code instead of a piece of text. Then to evaluate the expression, which is performing the divisions in this case, I have employed the eval() function.

To repeat the process for each element in `frac_char`

, I have used the sapply() function. Lastly, the unname() function was called to remove the names of the returned vector by the sapply() function.

## Video & Further Resources

Do you need further info on the examples of this tutorial? Then I can recommend having a look at the following video on my YouTube channel. I explain the R programming codes of this page in the video:

*The YouTube video will be added soon.*

In addition, you may want to have a look at the other tutorials on this website.

- Convert Decimals to Reduced Fractions in R
- parse, deparse & expression Functions in R
- R eval Function
- apply Functions in R
- Introduction to R

At this point you should know how to **change the fractional quantities to decimals** in R. Donâ€™t hesitate to let me know in the comments section below, if you have any further questions.

This page was created in collaboration with Cansu Kebabci. Have a look at Cansuâ€™s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.