I'm trying to create a conditional sum, in order to calculate an average. The idea is that an function (or an apply statement) checks if an certain value is true (for example x > 0), then sums all the values of x that where higher than zero. The last step would be to divide this sum by the number of instances which are greater than zero. Searching for conditonal sum(ming) didn't gave me usable information.
This is a part of the data:
> tmpData
Instrument TradeResult.Currency.
1 JPM -3
2 JPM 264
3 JPM 284
4 JPM 69
5 JPM 283
11 KFT -8
12 KFT -48
13 KFT 125
14 KFT -150
15 KFT -206
16 KFT 107
Of the functions that I've tried, the following holds the most promise:
avgProfit <- function(x) {
ifelse(x > 0,
sum(x) / length(which(x > 0)),
return(0))
}
However, the output of this function is 0:
> with(tmpData, tapply(TradeResult.Currency., Instrument, avgProfit))
JPM KFT
0 0
> avgProfit(tmpData$TradeResult.Currency.)
[1] 0
> x
[1] 1 1 2 1 2 3 3 3 4 4
(The values should be 225 for JPM (total of 900 divided by 4 instances which where greater than zero) and 116 for KFT)
Even though I calculate the sum of x (which, if I understand correctly, should be the sum of the individual values in the data.frame) in the function, the output of the variable 'x' puzzles me. I can't find where these 1,2,3 and fours are coming from.
How can I calculate an conditional sum? Besides, do I need to use an function or am I making it too complicated (perhaps there is an build-in R function for this which I overlooked?)
Any thoughts are more than welcome,
Regards,