[R] Using as.polynomial() over a matrix

MacQueen, Don
Wed, 06 Oct 2010 03:22:33 -0000

The key is in the help page for apply. It says (in part):


    In all cases the result is coerced by ?as.vector? to one of the
     basic vector types before the dimensions are set, so that (for
     example) factor results will be coerced to a character array.

So although as.polynomial() returns an object of class polynomial, apply
then runs as.vector() on it, which makes it not a polynomial class any more.

You should also be running apply in the first index (1) not the second (2),
as in
   apply(  whatever, 1, function )
If you want it to apply the function to each row, which I think you do. The
strange result you get is also explained by the help page:

    If each call to ?FUN? returns a vector of length ?n?, then ?apply?
     returns an array of dimension ?c(n, dim(X)[MARGIN])? if ?n > 1?.

This example will get you closer:

> tmp <- matrix(1:12, ncol=3, byrow=TRUE)
> foo <- apply(tmp,1, list)
> lapply(foo,function(x) as.polynomial(unlist(x)))
[[1]]
1 + 2*x + 3*x^2

[[2]]
4 + 5*x + 6*x^2

[[3]]
7 + 8*x + 9*x^2

[[4]]
10 + 11*x + 12*x^2

-Don


On 10/4/10 6:10 PM, "Raznahan, Armin (NIH/NIMH) [E]"
<raznahana at mail.nih.gov> wrote:

> Hello All
>
> First - a warning. I'm not very R or programming savvy.
>
> I am trying to do something without much luck, and have scoured help-pages,
> but nothing has come up. Here it is:
>
> I have a matrix (m) of approx 40,000 rows and 3 columns, filled with
numbers.
>
> I would like to convert the contents of this matrix into another matrix
(m_p),
> where the numbers of (m) have been coerced into a polynomial - using a
> function called "as.polynomial()" from the package (polynom).
Each row of (m)
> contains 3 terms to be made into a polynomial in the equivalent row of
(m_p).
>
> I have tried a coupe of things:
>
> ------------------------------
> 1. Using apply()
>
> m_p<-apply(m, 2, as.polynomial)
>
> Here is what happens..
>
>> dim(m)
> [1] 40962     3
>> m_p<-apply(m, 2, as.polynomial)
>> m_p[1:5,]
>             dM_I    dM_a.c dM_a.c.sq
> [1,] -0.00593058 -0.000688  3.65e-05
> [2,] -0.01913294  0.000103  1.41e-04
> [3,] -0.01317958 -0.001190  1.49e-04
> [4,] -0.02651112 -0.001550  2.37e-04
> [5,] -0.01680289 -0.003520  2.86e-04
>
> So - looks like the coercion hasn't worked. BUT, if I do things
piecemeal - it
> looks ok..
>
>> m_p1<-as.polynomial(m[1,])
>> m_p1
> -0.00593058 - 0.000688*x + 3.65e-05*x^2
> --------------------------------
> -------------------------------
> 2. This made me think I was making some wrong assumptions using apply(). So
I
> wrote a function "test()", to take each row of (m) , use
as.polynomial() on
> it,  and stick the results into a new matrix, which it would then return..
>
> test<-function(x){
> a<-nrow(x)
> b<-ncol(x)
> c<-matrix(0, a, b)
> for (i in 1:a) {
> c[i,]<-as.polynomial(x[i,]) }
> return (c)
> }
>
>> m_p<-test(m)
>> dim(m_p)
> [1] 40962     3
>> m_p[1:5,]
>             [,1]      [,2]     [,3]
> [1,] -0.00593058 -0.000688 3.65e-05
> [2,] -0.01913294  0.000103 1.41e-04
> [3,] -0.01317958 -0.001190 1.49e-04
> [4,] -0.02651112 -0.001550 2.37e-04
> [5,] -0.01680289 -0.003520 2.86e-04
>
> -------------------
>
> I don't know why I can do what I want when taking each line at a time,
but not
> when trying to run through the whole matrix.
>
> Sorry if missing something obvious.  Any help/pointers would be very
> gratefully received
>
> Thanks v much
>
> Armin
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://BLOCKEDstat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://BLOCKEDwww.BLOCKEDR-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>