有条件地应用功能 [英] Apply function conditionally

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

问题描述

我有一个这样的数据框:

I have a dataframe like this:

experiment iter  results
    A       1     30.0
    A       2     23.0
    A       3     33.3
    B       1     313.0
    B       2     323.0
    B       3     350.0
 ....

有没有一种方法可以通过应用带条件的函数来计算结果。在上面的示例中,该条件是特定实验的所有迭代。

Is there a way to tally results by applying a function with conditions. In the above example, that condition is all iterations of a particular experiment.

A   sum of results (30 + 23, + 33.3)
B   sum of results (313 + 323 + 350)

我在想应用功能,但找不到使它正常工作的方法。

I am thinking of "apply" function, but can't find a way to get it work.

推荐答案

有很多替代方法可以做到这一点。请注意,如果您对不同于 sum 的另一个函数感兴趣,则只需更改参数 FUN = any.function ,例如,如果您想要平均值 var 长度等,只需将这些函数插入 FUN 参数,例如, FUN = mean FUN = var 等。让我们探讨一些替代方法:

There are a lot of alternatives to do this. Note that if you are interested in another function different from sum, then just change the argument FUN=any.function, e.g, if you want mean, var length, etc, then just plug those functions into FUN argument, e.g, FUN=mean, FUN=var and so on. Let's explore some alternatives:

汇总基本函数。

> aggregate(results ~ experiment, FUN=sum, data=DF)
  experiment results
1          A    86.3
2          B   986.0






或者也许 tapply 吗?

> with(DF, tapply(results, experiment, FUN=sum))
    A     B 
 86.3 986.0 






也来自plyr软件包的 ddply

> # library(plyr)
> ddply(DF[, -2], .(experiment), numcolwise(sum))
  experiment results
1          A    86.3
2          B   986.0

> ## Alternative syntax
> ddply(DF, .(experiment), summarize, sumResults = sum(results))
  experiment sumResults
1          A       86.3
2          B      986.0






dplyr 软件包

> require(dplyr)
> DF %>% group_by(experiment) %>% summarise(sumResults = sum(results))
Source: local data frame [2 x 2]

  experiment  sumResults
1          A        86.3
2          B       986.0






使用 sapply split 等效于 tapply

> with(DF, sapply(split(results, experiment), sum))
    A     B 
 86.3 986.0 






如果您担心时间安排, data.table 是您的朋友:

> # library(data.table)
> DT <- data.table(DF)
> DT[, sum(results), by=experiment]
   experiment    V1
1:          A  86.3
2:          B 986.0






不是很流行,但是doBy包很好(相当于聚合 ,甚至在语法上也是如此!)


Not so popular, but doBy package is nice (equivalent to aggregate, even in syntax!)

> # library(doBy)
> summaryBy(results~experiment, FUN=sum, data=DF)
  experiment results.sum
1          A        86.3
2          B       986.0






在这种情况下, by 也有帮助

> (Aggregate.sums <- with(DF, by(results, experiment, sum)))
experiment: A
[1] 86.3
------------------------------------------------------------------------- 
experiment: B
[1] 986

如果希望结果为矩阵,则使用 cbind rbind

If you want the result to be a matrix then use either cbind or rbind

> cbind(results=Aggregate.sums)
  results
A    86.3
B   986.0






sqldf 也可能是一个不错的选择


sqldf from sqldf package also could be a good option

> library(sqldf)
> sqldf("select experiment, sum(results) `sum.results`
      from DF group by experiment")
  experiment sum.results
1          A        86.3
2          B       986.0






xtabs 也有效(仅当 FUN = sum 时)

> xtabs(results ~ experiment, data=DF)
experiment
    A     B 
 86.3 986.0

这篇关于有条件地应用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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