Here is some code I wrote to generate a correlation matrix and plot for a list of symbols. In my example, I am pulling the data out of my database, but you could also use the quantmod library and getSymbols() function to get the data from a public source like Yahoo if you need to.
First thing I do is pull a meta data into a data frame:
This gives us a subset (32 values) of just the tickers where the avg daily volume is >20M. Here is a random sample of that data:
> myTickers[sample(nrow(myTickers), 5), ]
Symbol Date Close AvgDailyVolume AvgDailyRange
1768 GM 2013-11-25 37.55 23496904 0.62200
1462 EWJ 2013-11-25 12.07 29128785 0.06497
2914 NOK 2013-11-25 8.10 30859016 0.14528
4590 ZNGA 2013-11-25 4.50 26274119 0.15775
3836 SPY 2013-11-25 180.63 104524325 1.08268
Now we run it through the bulkCorrelation() function, passing myTickers$Symbol as the symbol name list, and I'll specify 120 as the number of days I want in the series, and that I want to run this against my "daily" database of OHLCV data.
> system.time(mx plotCorrelationMatrix(mx)
The scale is from -1 (perfect negative correlation) to +1 (perfect positive correlation). The sweet spot is 0, no correlation.
If you run this against a large number of instruments the heatmap isn't too useful, so instead we can rely on the function getCorrelationMatrixMins() to give us the minimum correlation value for each pair, as follows: