71x2446 データ フレームを取り、2x2446 倍精度行列を返す関数結果があります。2446 組のそれぞれの最初の数字は 1 ~ 6 の整数を表し、これは基本的に線がどのカテゴリに当てはまるかを表し、2 番目の数字はそのカテゴリの利益または損失です。各カテゴリの頻度をカウントしながら、各カテゴリの利益の合計を計算したいと考えています。私の質問は、私が書いた方法がベクトルの効率的な使用であるかどうかです
vec<-as.data.frame(t(apply(theData,1,theresults)))
vec[2][vec[1]==1]->successCrossed
vec[2][vec[1]==2]->failCrossed
vec[2][vec[1]==3]->successFilled
vec[2][vec[1]==4]->failFilled
vec[2][vec[1]==5]->naCount
vec[2][vec[1]==6]->otherCount
次に、結果を要約している間に、 length() と mean() への呼び出しがたくさんあります。
結果は、このような方法で元のデータ フレームを参照します
theresults<-function(theVector)
{
if(theVector[['Aggressor']]=="Y")
{
if(theVector[['Side']]=="Sell")
{choice=6}
else
{choice=3}
if(!is.na(theVector[['TradePrice']])&&!is.na(theVector[['L1_BidPri_1']])&&!is.na(theVector[['L1_AskPri_1']])&&!is.na(theVector[['L2_BidPri_1']])&&!is.na(theVector[['L2_AskPri_1']]))
{
Profit<- switch(choice,
-as.numeric(theVector[['TradePrice']]) + 10000*as.numeric(theVector[['L1_AskPri_1']])/as.numeric(theVector[['L2_BidPri_1']]),
-as.numeric(theVector[['TradePrice']]) + 10000*as.numeric(theVector[['L1_BidPri_1']])/as.numeric(theVector[['L2_BidPri_1']]),