Raznahan, Armin (NIH/NIMH) [E]
2010-Oct-05 01:10 UTC
[R] Using as.polynomial() over a matrix
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
it looks like as. polynomial merely stores the coefficients of the polynomial. "Internally, polynomials are simply numeric coefficient vectors of class "polynomial". " If you really want to print it in the form you want try this. m <- matrix(rnorm(3000),1000,3) apply(m, 1, function(x){print(as.polynomial(x))}) # Not sure why you are using 2 i.e. applying the formula column wise instead of 1, row wise. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.list@gmail.com On Oct 4, 2010, at 9:10 PM, Raznahan, Armin (NIH/NIMH) [E] 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@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.[[alternative HTML version deleted]]
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. >