Hi, I need to multiply all columns in a matrix so something like apply(x,2,sum), but using multiplication should do. I have tried apply(x,2,"*") I know this must be trivial, but I get: Error in FUN(newX[, i], ...) : invalid unary operator The help for apply states that unary operators must be quoted. I tried single quotes too, with the same results. Thanks, -Jose -- Jose Quesada, PhD. http://www.andrew.cmu.edu/~jquesada
Henrique Dallazuanna
2007-May-07 18:32 UTC
[R] like apply(x,1,sum), but using multiplication?
Try: apply(x, 2, prod) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O<http://maps.google.com/maps?f=q&hl=en&q=Curitiba,+Brazil&layer=&ie=UTF8&z=18&ll=-25.448315,-49.276916&spn=0.002054,0.005407&t=k&om=1> On 07/05/07, Jose Quesada <quesada@gmail.com> wrote:> > Hi, > > I need to multiply all columns in a matrix so something like > apply(x,2,sum), but using multiplication should do. > I have tried apply(x,2,"*") > I know this must be trivial, but I get: > Error in FUN(newX[, i], ...) : invalid unary operator > > The help for apply states that unary operators must be quoted. I tried > single quotes too, with the same results. > > Thanks, > -Jose > > -- > Jose Quesada, PhD. > http://www.andrew.cmu.edu/~jquesada > > ______________________________________________ > R-help@stat.math.ethz.ch 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]]
Benilton Carvalho
2007-May-07 18:32 UTC
[R] like apply(x,1,sum), but using multiplication?
see ?prod b On May 7, 2007, at 2:25 PM, Jose Quesada wrote:> Hi, > > I need to multiply all columns in a matrix so something like > apply(x,2,sum), but using multiplication should do. > I have tried apply(x,2,"*") > I know this must be trivial, but I get: > Error in FUN(newX[, i], ...) : invalid unary operator > > The help for apply states that unary operators must be quoted. I tried > single quotes too, with the same results. > > Thanks, > -Jose > > -- > Jose Quesada, PhD. > http://www.andrew.cmu.edu/~jquesada > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
Jose Quesada said the following on 5/7/2007 11:25 AM:> Hi, > > I need to multiply all columns in a matrix so something like > apply(x,2,sum), but using multiplication should do. > I have tried apply(x,2,"*") > I know this must be trivial, but I get: > Error in FUN(newX[, i], ...) : invalid unary operator > > The help for apply states that unary operators must be quoted. I tried > single quotes too, with the same results. > > Thanks, > -Jose >Try: apply(x,2,prod) HTH, --sundar