Learn R Programming

fractional (version 0.1.3)

numerical: Convert a fractional object to the equivalent numeric object

Description

Convert an object of class "fractional" or "charFrac" to a purely numeric object. This is effectively a method function for the .Primitive generic function as.numeric but written as a separate function for purely technical reasons.

Usage

numerical(vulgar)
"numerical"(vulgar)
"numerical"(vulgar)
"numerical"(vulgar)

Arguments

vulgar
character string form of a class 'fractional' object.

Value

A numeric object as represented by its (usually fractional) display.

Methods (by class)

  • fractional: Method for "fractional" objects
  • charFrac: Method for "charFrac" objects
  • default: Default method for numerical generic

Examples

Run this code
suppressPackageStartupMessages(library(dplyr))
m <- 2*diag(5)
m[abs(row(m) - col(m)) == 1] <- -1
m  ## How much roundoff error does inverting entail?
(mi  <- solve(m) %>% fractional) ## patterned inverse
mi * max(denominators(mi)) ## clearer pattern
m1 <- solve(mi)
range(m1 - m)           ## roundoff still present
m2 <- m1 %>% numerical  ## remove roundoff error - hopefully!
identical(m2, m)        ## no roundoff

Run the code above in your browser using DataLab