I have a function theresults which takes a 71x2446 data frame and returns a 2x2446 double matrix. the first number in each of the 2446 pairs represents an integer 1-6, which is basically what category the line fits into, and the second number is the Profit or Loss on that category. I want to calculate the sum of profits across each category while counting the frequency of each category. My question is if the way I've written it is an efficient use of vectors
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
then there are a bunch of calls to length() and mean() while summarizing the results.
theresults references the original data frame in this sort of way
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']]),