2017-11-18 1 views
1

임의의 길이의 매개 변수 벡터를 하나의 논리 계산에 결합하려고합니다. 기본적으로, 계산하기 위해 노력하고 있습니다 :R : 임의의 수의 함수와 매개 변수를 하나의 계산에 결합하는 방법

IndicatorA <- c(30.30815, 30.39961, 30.47286) 
IndicatorB <- c(30.39961, 30.47286, 30.519) 
BuyConditions <- c(">", ">", ">") 
BuySigConditions <- c("&", "&") 

및 기능 sigBuy() : 다음 매개 변수 세트에서

get("&")(get("&")(sigBuy(">", 
       30.30815, 
       30.39961), 
      sigBuy(">", 
       30.39961, 
       30.47286)), 
      sigBuy(">", 
       30.47286, 
       30.519)) 

sigBuy <- function(BuyCondition, IndicatorA, IndicatorB) { 
    out <- get(get("BuyCondition"))(IndicatorA, IndicatorB) 
    return(out) 
} 

제가 적용하려고 실제 로직 이 경우에는

IndicatorA[1] > IndicatorB[1] & 
    IndicatorA[2] > IndicatorB[2] & 
    IndicatorA[3] > IndicatorB[3] 
IndicatorA, IndicatorB, BuyConditions 또는 BuySigConditions의 길이는 IndicatorA, IndicatorB, & BuyConditions가 동일하고 나머지 BuySigConditions의 길이가 나머지 하나보다 작도록 제한하는 것 이외에는 매개 변수의 길이를 모릅니다. . 또한 BuyConditions는 <,>, < =,> =, ==,! = 중 하나 일 수 있으며 BuySigConditions는 & 또는 | 일 수 있습니다. 나는 사용자가 임의의 길이의 벡터 매개 변수 (위의 길이 제약 조건에 따라)를 전달할 논리를 기능화 할 것이며,이 함수의 결과는 다음 예제와 같이이 예제의 결과로 얻으려고 시도합니다.

답변

1

do.callmapply이 여기에 도움이 될 수 있습니다.

sigBuy <- function(cond, ...) do.call(cond, list(...)) 
reln <- mapply(sigBuy, BuyConditions, IndicatorA, IndicatorB) 

result <- reln[1] 
for(i in seq_along(BuySigConditions)) 
    result <- do.call(BuySigConditions[i], list(result, reln[i+1])) 
result 
+0

감사합니다. –

관련 문제