Convert decimals to a matrix of numerators and denominators

frac_mat(
  x,
  ...,
  denom = NULL,
  base_10 = FALSE,
  common_denom = FALSE,
  mixed = FALSE,
  max_denom = 1e+07
)

as.frac_mat(x)

is.frac_mat(x)

Arguments

x

A vector of decimals or, for as.frac_mat(), a character vector created by fracture()

...

These dots are for future extensions and must be empty.

denom

If denom is not NULL, all fractions will have a denominator of denom. This will ignore all other arguments that affect the denominator.

base_10

If TRUE, all denominators will be a power of 10.

common_denom

If TRUE, all fractions will have the same denominator.

If the least common denominator is greater than max_denom, max_denom is used.

mixed

If TRUE, integer components will be displayed separately from fractional components for x values greater than 1.

If FALSE, improper fractions will be used for x values greater than 1.

max_denom

All denominators will be less than or equal to max_denom.

If base_10 is TRUE, the maximum denominator will be the largest power of 10 less than max_denom.

A max_denom greater than the inverse square root of machine double epsilon will produce a warning because floating point rounding errors can occur when denominators grow too large.

Value

A matrix with the same number of columns as the length of x and rows for integers (if mixed is TRUE), numerators, and denominators.

See also

fracture() to return a character vector of fractions.

Examples

x <- (6:1) / (1:6)

frac_mat(x)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> numerator      6    5    4    3    2    1
#> denominator    1    2    3    4    5    6
frac_mat(x, common_denom = TRUE)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> numerator    360  150   80   45   24   10
#> denominator   60   60   60   60   60   60

frac_mat(x, base_10 = TRUE)
#>             [,1] [,2]     [,3] [,4] [,5]     [,6]
#> numerator      6   25 13333333   75    4  1666667
#> denominator    1   10 10000000  100   10 10000000
frac_mat(x, base_10 = TRUE, max_denom = 100)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> numerator      6   25  133   75    4   17
#> denominator    1   10  100  100   10  100
frac_mat(x, base_10 = TRUE, common_denom = TRUE)
#>              [,1]    [,2]     [,3]    [,4]  [,5]     [,6]
#> numerator   6e+07 2.5e+07 13333333 7.5e+06 4e+06  1666667
#> denominator 1e+07 1.0e+07 10000000 1.0e+07 1e+07 10000000
frac_mat(x, base_10 = TRUE, common_denom = TRUE, max_denom = 100)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> numerator    600  250  133   75   40   17
#> denominator  100  100  100  100  100  100

frac_mat(x, mixed = TRUE)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> integer        6    2    1    0    0    0
#> numerator      0    1    1    3    2    1
#> denominator    1    2    3    4    5    6
frac_mat(x, mixed = TRUE, common_denom = TRUE)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> integer        6    2    1    0    0    0
#> numerator      0   30   20   45   24   10
#> denominator   60   60   60   60   60   60
frac_mat(x, mixed = TRUE, base_10 = TRUE)
#>             [,1] [,2]     [,3] [,4] [,5]     [,6]
#> integer        6    2        1    0    0        0
#> numerator      0    5  3333333   75    4  1666667
#> denominator    1   10 10000000  100   10 10000000
frac_mat(x, mixed = TRUE, base_10 = TRUE, max_denom = 100)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> integer        6    2    1    0    0    0
#> numerator      0    5   33   75    4   17
#> denominator    1   10  100  100   10  100
frac_mat(x, mixed = TRUE, base_10 = TRUE, common_denom = TRUE)
#>              [,1]  [,2]     [,3]    [,4]  [,5]     [,6]
#> integer     6e+00 2e+00        1 0.0e+00 0e+00        0
#> numerator   0e+00 5e+06  3333333 7.5e+06 4e+06  1666667
#> denominator 1e+07 1e+07 10000000 1.0e+07 1e+07 10000000
frac_mat(x, mixed = TRUE, base_10 = TRUE, common_denom = TRUE, max_denom = 100)
#>             [,1] [,2] [,3] [,4] [,5] [,6]
#> integer        6    2    1    0    0    0
#> numerator      0   50   33   75   40   17
#> denominator  100  100  100  100  100  100