[R] Rmpfr into data.frame

Martin Maechler maechler at stat.math.ethz.ch
Mon Nov 7 12:00:19 CET 2016


>>>>> Troels Ring <tring at gvdnet.dk>
>>>>>     on Mon, 7 Nov 2016 10:09:02 +0100 writes:

    > Dear friends - Windows, R version 3.2.1
    > I wanted to make a ggplot2 using Rmpfr high precision data - but cannot 
    > make the data into a data.frame, as wanted by ggplot2. Hence

    > library(Rmpfr)
    > a <- mpfr(1,120)
    > b <- mpfr(2,120)
    > dff <- data.frame(a=a,b=b)

    > elicits errors

    > Error in as.data.frame.default(x[[i]], optional = TRUE) :
    > cannot force class »structure("mpfr1", package = "Rmpfr")« into 
    > data.frame

ggplot2 nor any other "R-based" (*) plotting engine can make use
of the extra precision in the mpfr numbers.

So for plotting (and other purposes), there's the   asNumeric(.)
function in Rmpfr, which keeps Rmpfr - arrays or matrices.
For the present case (of mpfr-vectors with no attributes), you
can also use  as.numeric() :

> (Pi <- Const("pi"))
1 'mpfr' number of precision  120   bits 
[1] 3.1415926535897932384626433832795028847
> asNumeric(Pi)
[1] 3.141593
> all.equal(pi, asNumeric(Pi), tol=1e-17)
[1] TRUE
> identical(asNumeric(Pi), as.numeric(Pi))
[1] TRUE
> 

and your case
    data.frame(a=as.numeric(a), b=as.numeric(b))

As author and maintainer of Rmpfr, I'm curious why you'd be
interested to use high-precision numbers in plots, because even
double precision is usually far from being needed for graphics.

Martin Maechler, ETH Zurich

--
*) in the sense of either relying on "base R"s packages 'graphics' or 'grid';
   the latter is used by lattice and ggplot2.


    > (my translation of Danish error)

    > How can I do it best?



More information about the R-help mailing list