Replace list of permutations with getSymbols data in R

81 views Asked by At

I downloaded some stock data:

require("quantmod")
s <- c("AAPL", "ADBE", "ADI", "ADP", "ADSK") 
e <- new.env()
getSymbols(s, src='yahoo',  from='2015-01-10',  env = e )

#get closing prices
close <- do.call(merge, eapply(e, function(x) Cl(x)))

I found all the pairs of symbol names:

#find all the pairwise permutations
perm<-combn(s,2)
perm
[,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]  [,9]   [,10] 
[1,] "AAPL" "AAPL" "AAPL" "AAPL" "ADBE" "ADBE" "ADBE" "ADI" "ADI"  "ADP" 
[2,] "ADBE" "ADI"  "ADP"  "ADSK" "ADI"  "ADP"  "ADSK" "ADP" "ADSK" "ADSK"

I'd like to take the "close" data for each name and replace for each pair group of "perm" to be able to analyse the pairs later.

1

There are 1 answers

0
josliber On BEST ANSWER

If you wanted to get a list of data frames, one for each pair, you could try:

dfs <- lapply(seq_len(ncol(perm)), function(x) close[,paste0(perm[,x], ".Close")])

Now you can get the 2-column data frames for each pair with dfs[[1]], dfs[[2]], etc. You can perform statistical analyses on each pair using the lapply function. Here's a simple example where you get the summary of every paired data frame:

lapply(dfs, summary)