R - Passing the results of the Combn function with two parameters

I have a stock symbol vector / etf that varies depending on my interests.

Example:

symbols_v <- c('UPRO','TLT','SPXU','TBT','DRN','URE','SOXL') 

I would like to use the combination rule, two at a time for iterating over a vector, for generating pairs.

Example:

 p <- combn(symbols_v, 2) 

Each pair of p must be transferred to a (user-defined) function with two parameters " f(x,y) ", which will load the stock data and perform correlations and other functions.

My questions are: 1) What would be the best data structure for p for step 2 below? 2) Given the data structure p, what is the easiest way to parse it in order to pass pairs to the function f?

Summary: how to take the results of the combn() function and pass each pair to the f(x,y) function?

I am trying to minimize the loop since this is considered "slow" in R.

I am probably absent from something fundamental, but I apparently cannot help but hug him around.

Please feel free to insult my intelligence by providing an answer :)

+4
source share
4 answers

mapply looks like what you are looking for. It allows you to provide a function and multiple inputs - it will use the first element of each of your inputs for the function, then the second element of inputs, etc.

 symbols_v <- c('UPRO','TLT','SPXU','TBT','DRN','URE','SOXL') out <- combn(symbols_v, 2) # What you would probably want mapply(f, out[1,], out[2,]) # Example output mapply(paste, out[1,], out[2,]) # get rid of names mapply(paste, out[1,], out[2,], USE.NAMES = FALSE) # add other parameters to function of interest mapply(paste, out[1,], out[2,], USE.NAMES = FALSE, MoreArgs = list(sep = ".")) 

The result of these examples:

 > mapply(paste, out[1,], out[2,]) UPRO UPRO UPRO UPRO UPRO UPRO "UPRO TLT" "UPRO SPXU" "UPRO TBT" "UPRO DRN" "UPRO URE" "UPRO SOXL" TLT TLT TLT TLT TLT SPXU "TLT SPXU" "TLT TBT" "TLT DRN" "TLT URE" "TLT SOXL" "SPXU TBT" SPXU SPXU SPXU TBT TBT TBT "SPXU DRN" "SPXU URE" "SPXU SOXL" "TBT DRN" "TBT URE" "TBT SOXL" DRN DRN URE "DRN URE" "DRN SOXL" "URE SOXL" > # get rid of names > mapply(paste, out[1,], out[2,], USE.NAMES = FALSE) [1] "UPRO TLT" "UPRO SPXU" "UPRO TBT" "UPRO DRN" "UPRO URE" "UPRO SOXL" [7] "TLT SPXU" "TLT TBT" "TLT DRN" "TLT URE" "TLT SOXL" "SPXU TBT" [13] "SPXU DRN" "SPXU URE" "SPXU SOXL" "TBT DRN" "TBT URE" "TBT SOXL" [19] "DRN URE" "DRN SOXL" "URE SOXL" > # add other parameters to function of interest > mapply(paste, out[1,], out[2,], USE.NAMES = FALSE, MoreArgs = list(sep = ".")) [1] "UPRO.TLT" "UPRO.SPXU" "UPRO.TBT" "UPRO.DRN" "UPRO.URE" "UPRO.SOXL" [7] "TLT.SPXU" "TLT.TBT" "TLT.DRN" "TLT.URE" "TLT.SOXL" "SPXU.TBT" [13] "SPXU.DRN" "SPXU.URE" "SPXU.SOXL" "TBT.DRN" "TBT.URE" "TBT.SOXL" [19] "DRN.URE" "DRN.SOXL" "URE.SOXL" 
+3
source

You can simply use the FUN combn argument:

 library(quantmod) # for getSymbols symbols_v <- c('UPRO','TLT','SPXU') # shorter example # simple function to download data and calculate correlation between close prices f <- function(x) { x1 <- getSymbols(x[1], auto.assign=FALSE) x2 <- getSymbols(x[2], auto.assign=FALSE) y <- merge(Cl(x1),Cl(x2)) cor(y[,1],y[,2],use="complete.obs") } # run 'f' on each pair p <- combn(symbols_v, 2, FUN=f, simplify=FALSE) [[1]] TLT.Close UPRO.Close -0.6394617 [[2]] SPXU.Close UPRO.Close 0.0947242 [[3]] SPXU.Close TLT.Close -0.06216682 
+10
source

You can use apply(combn(symbols_v, 2),2,function(x){f(x[1],x[2])})

+1
source

combn also allows you to pass multiple arguments to the called function. The first argument will be passed

 fun <- function (x1, x2) someOperation y <- someData combn(rnorm(1:10), 2, fun, x2=y) 
0
source

Source: https://habr.com/ru/post/1434236/


All Articles