0

so I have a data frame containing certain type of data for different stocks, sample as followed:

Date     RY    TD     BNS...
10-01   2.98   2.29   1.91
10-02   2.96   2.61   2.15
10-03   2.96   2.59   2.09
...

What I want to do is to use combn() function to calculate the sum of the products of all possible combinations of 2 stocks. I know how to do it with single values using, for example:

df <- c(2.98, 2.29, 1.91)
sum(combn(df, 2, prod))

But since now I have a data frame with daily data for each symbol, how do I apply the above function and output the sum results as a data list corresponding to each date?

Thanks

  • 1
    I think it needs to be `apply(df, 1, ...)`: i.e. `apply(df[,-1], 1, function(x){sum(combn(x, 2, prod))})` – alistaire May 12 '16 at 17:04

1 Answers1

0

As suggested by Alistaire, apply can be easily used for this

apply(df[, -1], 1, function(x) {
     sum(combn(x, 2, prod))
}
)

You could also use package parallel and use mcapply* functions to run it in multicore modes

sachinv
  • 492
  • 2
  • 5
  • 18