[R] Using as.polynomial() over a matrix
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Wed Oct 6 07:35:06 CEST 2010
PS ... actually, now I think about it...
> as.polylist(apply(m, 1, polynom))
List of polynomials:
[[1]]
1 + 5*x + 9*x^2 + 13*x^3
[[2]]
2 + 6*x + 10*x^2 + 14*x^3
[[3]]
3 + 7*x + 11*x^2 + 15*x^3
[[4]]
4 + 8*x + 12*x^2 + 16*x^3
This works with the PolynomF package because in that case the objects are functions and simplification is not possible. Another reason to leave polynomial behind and switch.
Bill Venables.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bill.Venables at csiro.au
Sent: Wednesday, 6 October 2010 2:07 PM
To: macqueen1 at llnl.gov; raznahana at mail.nih.gov; r-help at r-project.org
Subject: [ExternalEmail] Re: [R] Using as.polynomial() over a matrix
I was unaware of this discussion till now, so I'm not up with what's been said already.
Here is how I would go about the problem I think you are trying to solve.
> m <- matrix(1:16, 4, 4)
> require(PolynomF)
> (lop <- as.polylist(lapply(1:nrow(m), function(i) polynom(m[i,]))))
List of polynomials:
[[1]]
1 + 5*x + 9*x^2 + 13*x^3
[[2]]
2 + 6*x + 10*x^2 + 14*x^3
[[3]]
3 + 7*x + 11*x^2 + 15*x^3
[[4]]
4 + 8*x + 12*x^2 + 16*x^3
> plot(lop) # reasonably dull plot...
>
Note that the package PolynomF is intended to replace polynomial. The latter is just there for backward compatibility reasons.
Bill Venables.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of MacQueen, Don
Sent: Wednesday, 6 October 2010 1:23 PM
To: Raznahan, Armin (NIH/NIMH) [E]; r-help at r-project.org
Subject: Re: [R] Using as.polynomial() over a matrix
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.
>
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list