[R] question about Rmpfr use in *apply and list output

Carl Witthoft carl at witthoft.com
Fri Jul 8 00:52:34 CEST 2011


I am trying to use the family of apply functions on mpfr1-class 
variables, but run into problems with the output.  Consider a simple 
example:

foo <- sapply(1:15,FUN=function(x) sin(mpfr(x,20)))

foo is a list, each element of which contains a mpfr1-class value. 
What I don't understand is why unlist(foo) does nothing (so far as I can 
tell).  Since foo is a list, I can't do nice things like

sum(foo) ,  and

Rgames> sum(unlist(foo))
Error in sum(unlist(foo)) : invalid 'type' (list) of argument

By comparison,

Rgames> bar <- sin(mpfr(1:15,20))
Rgames> class(bar)
[1] "mpfr"
attr(,"package")
[1] "Rmpfr"

Rgames> sum(bar)
1 'mpfr' number of precision  53   bits
[1] 1.9356861114501953

So:  is there a simple way either to force sapply not to create an 
output that's a list containing one value per element but rather a list 
with one element containing all the values?  (which is what the variable 
'bar' is in my example).

Or failing that, is there a function that can collect all the values in 
all the elements of 'foo' above into a single list element?

thanks for advice and corrections.

Carl
-- 
-----
Sent from my Cray XK6



More information about the R-help mailing list