条件求和(R) [英] Conditional summing (R)

查看:81
本文介绍了条件求和(R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建条件和,以计算平均值。这个想法是一个函数(或一条apply语句)检查某个值是否为真(例如x> 0),然后将x大于零的所有值相加。最后一步是将该总和除以大于零的实例数。搜索条件对数(ming)没有提供有用的信息。

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.

这是数据的一部分:

> 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))
    }

但是,此函数的输出为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

(值对于JPM,应该为225(900的总数除以4个大于零的实例),对于KFT应该为116。

(The values should be 225 for JPM (total of 900 divided by 4 instances which where greater than zero) and 116 for KFT)

即使我计算了x的总和(如果我理解正确,应该是函数中data.frame中各个值的总和,变量 x的输出使我感到困惑。我找不到这些1,2,3和4的来源。

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.

如何计算条件和?此外,我需要使用一个函数还是使其变得过于复杂(也许有一个内置的R函数被我忽略了?)

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?)

任何想法

问候

推荐答案

简单的方法是先删除未使用的行,然后对其进行汇总:

probably it is easy way to drop unused rows first and then aggregate them:

aggregate(TradeResult.Currency.~Instrument,
  mean,
  data=subset(tmpData,TradeResult.Currency.>0))

这篇关于条件求和(R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆