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

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

问题描述

我正在尝试创建一个条件总和,以计算平均值.这个想法是一个函数(或一个 apply 语句)检查某个值是否为真(例如 x > 0),然后将 x 的所有大于零的值相加.最后一步是将这个总和除以大于零的实例数.搜索条件 sum(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天全站免登陆