mapply
似乎是你要找的。它允许您提供一个函数和多个输入 - 然后它将为函数使用每个输入的第一个元素,然后是输入的第二个元素,依此类推。
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 = "."))
这些示例的输出:
> 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"