0

I have matrix which has 120 columns and 7 rows in each. I want vectors in which every vector is vn where n is number of column, and it has every value of vector multiplied in "series".

For example, second column has values c(1, 2, 3, 4, 5, 6, 7). So I want vector v2, which would be 1*2*3*4*5*6*7 = 5040, so v2 = 5040. What is easiest way to do this?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Mikoyan
  • 19
  • 3
  • 1
    Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). But, `apply(yourmatrix, MARGIN=1, FUN=prod)` might do the job for you? – Heroka Oct 31 '15 at 20:55
  • 1
    for operate on columns `MARGIN=2` – jogo Oct 31 '15 at 21:10
  • 2
    To be a overly clever: `exp(colSums(log(yourmatrix)))`. – Gregor Thomas Oct 31 '15 at 21:16

1 Answers1

1

Try this:

yourmatrix <-matrix(1:14, ncol=120, nrow=7)
res <-apply(yourmatrix, MARGIN=2, FUN=prod)
as.data.frame(t(res), colnames(paste0("V",1:120)))

    V1       V2   V3       V4   V5       V6 ...
1 5040 17297280 5040 17297280 5040 17297280
Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56