r - 计算 apply 语句中的百分比 (R)

标签 r apply

我正在努力解决一些非常简单的事情,但我在原地打转,只是看不出我在哪里犯了错误。我真的希望有人能给我一个方便的建议,让我不再陷入困境!

我的目标:我想计算 data.frame 中结果高于 0 的实例的百分比。我已经用 for 循环尝试过此操作,但无济于事。因此,经过更多搜索后,我使用 apply 函数来计算各种指标,如平均值、标准差和最小值/最大值。这非常有效,但是对于计算百分比,应用函数不起作用,即使我制作自定义函数并将其插入到应用函数中也是如此。

这是我的 data.frame 的缩短版本:

     tradesList[c(1:5,10:15),c(1,7)]
   Instrument TradeResult.Currency.
1         JPM                    -3
2         JPM                   264
3         JPM                   284
4         JPM                    69
5         JPM                   283
10        JPM                  -294
11        KFT                    -8
12        KFT                   -48
13        KFT                   125
14        KFT                  -150
15        KFT                  -206

我想总结这个数据框,例如通过显示每个工具的平均 TradeResult:

> tapply(tradesList$TradeResult.Currency., tradesList$Instrument, mean)
 JPM  KFT 
42.3 14.6 

但是,我还想计算每个工具 TradeResult > 0 的行的百分比。但是,使用“which”函数检查 > 0 的实例确实可以工作,但 apply 不会接受此函数作为参数。

> length(which(tradesList$TradeResult.Currency. > 0)) / length(tradesList$TradeResult.Currency.) * 100
[1] 50
> tapply(tradesList$TradeResult.Currency., tradesList$Instrument, (length(which(tradesList$TradeResult.Currency. > 0)) / length(tradesList$TradeResult.Currency.) * 100))
Error in match.fun(FUN) : 
  c("'(length(which(tradesList$TradeResult.Currency. > 0))/length(tradesList$TradeResult.Currency.) * ' is not a function, character or symbol", "'    100)' is not a function, character or symbol")
> 

我在帮助函数中搜索了有关此错误的更多信息,并尝试了各种不同的方法来表达该函数(例如使用括号或引号),但每种方法都得到相同的结果。

有人知道如何计算大于零的实例的百分比吗?也许我错过了什么?

提前非常感谢,

问候,

编辑: 非常感谢 G. Grothendieck、Gavin Simpson 和 DWin 的快速评论。高度赞赏并且非常有帮助!

已解决: 这是我现在拥有的:

> tmpData <- tradesList[c(1:5,10:15),c(1,7)]
> tmpData
   Instrument TradeResult.Currency.
1         JPM                    -3
2         JPM                   264
3         JPM                   284
4         JPM                    69
5         JPM                   283
10        JPM                  -294
11        KFT                    -8
12        KFT                   -48
13        KFT                   125
14        KFT                  -150
15        KFT                  -206
> 100*    # to get percentages
+ with( tmpData, 
+ tapply( (TradeResult.Currency. > 0) , Instrument, sum)/   # number GT 0
+        tapply( TradeResult.Currency., Instrument, length) ) # total number
     JPM      KFT 
66.66667 20.00000 
> 100 * tapply(tmpData$TradeResult.Currency. > 0, tmpData$Instrument, mean)
     JPM      KFT 
66.66667 20.00000 
> pcentFun <- function(x) {
+     res <- x > 0
+     100 * (sum(res) / length(res))
+ }
> 
> with(tmpData, tapply(TradeResult.Currency., Instrument, pcentFun))
     JPM      KFT 
66.66667 20.00000

再次感谢!

问候,

最佳答案

编写一个简单的函数来进行计算:

pcentFun <- function(x) {
    res <- x > 0
    100 * (sum(res) / length(res))
}

然后我们可以通过tapply()将其应用于仪器组

> with(tradeList, tapply(TradeResult.Currency, Instrument, pcentFun))
     JPM      KFT 
66.66667 20.00000 

但是如果您想要带有仪器名称的摘要,aggregate() 会更有用:

> with(tradesList, aggregate(TradeResult.Currency, 
+                            by = list(Instrument = Instrument), pcentFun))
  Instrument        x
1        JPM 66.66667
2        KFT 20.00000

关于r - 计算 apply 语句中的百分比 (R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359595/

相关文章:

r - 有没有办法使用 par () 函数来排列绘图?

r - 为什么使用 quantmod 获取开盘交易价格会出现延迟

javascript - 如何对字符串数组使用 apply 方法?

javascript - 新建时使用可变参数

python - Pandas:如何在 groupby 对象上使用自定义应用函数返回多列

r - 标签乱序 (ggplot2)

r - 将多维 ctab() 表转换为 LaTeX 格式

R汇总函数的重组输出

r - 如何让 which.max 返回行名而不是​​索引号

JavaScript 应用()