用聚合解决 ddply 任务的优雅方法(希望有更好的性能)
我想通过名为 ensg
的标识符变量聚合 data.frame
。数据框如下所示:
chromosome probeset ensg symbol XXA_00 XXA_36 XXB_00
1 X 4938842 ENSMUSG00000000003 Pbsn 4.796123 4.737717 5.326664
我想计算具有相同 ensg
值的行上每个数字列的平均值。这里的问题是我想保持其他身份变量染色体和符号不变,因为它们对于相同的 ensg 也是相同的。
最后我想要一个带有标识列chromosome
、ensg
、symbol
和mean的data.frame
具有相同标识符的数字列覆盖行。我在 ddply 中实现了这一点,但与聚合相比它非常慢:
spec.mean <- function(eset.piece)
{
cbind(eset.piece[1,-numeric.columns],t(colMeans(eset.piece[,numeric.columns])))
}
t
mean.eset <- ddply(eset.consensus.grand,.(ensg),spec.mean,.progress="tk")
我的第一个聚合实现如下所示,
mean.eset=aggregate(eset[,numeric.columns], by=list(eset$ensg), FUN=mean, na.rm=TRUE);
并且速度要快得多。但聚合的问题是我必须重新附加描述变量。我还没有弄清楚如何将我的自定义函数与aggregate
一起使用,因为aggregate
不传递数据帧,而只传递向量。
有没有一种优雅的方法可以使用aggregate
来做到这一点?或者有没有更快的方法来使用 ddply 来做到这一点?
I would like to aggregate a data.frame
by an identifier variable called ensg
. The data frame looks like this:
chromosome probeset ensg symbol XXA_00 XXA_36 XXB_00
1 X 4938842 ENSMUSG00000000003 Pbsn 4.796123 4.737717 5.326664
I want to compute the mean for each numeric column over rows with same ensg
value. The problem here is that I would like to leave the other identity variables chromosome and symbol untouched as they are also the same for same ensg
.
In the end I would like to have a data.frame
with identity columns chromosome
, ensg
, symbol
and mean of numeric columns over rows with same identifier. I implemented this in ddply
, but it is very slow when compared to aggregate
:
spec.mean <- function(eset.piece)
{
cbind(eset.piece[1,-numeric.columns],t(colMeans(eset.piece[,numeric.columns])))
}
t
mean.eset <- ddply(eset.consensus.grand,.(ensg),spec.mean,.progress="tk")
My first aggregate implementation looks like this,
mean.eset=aggregate(eset[,numeric.columns], by=list(eset$ensg), FUN=mean, na.rm=TRUE);
and is much faster. But the problem with aggregate
is that I have to reattach the describing variables. I have not figured out how to use my custom function with aggregate
since aggregate
does not pass data frames but only vectors.
Is there an elegant way to do this with aggregate
? Or is there some faster way to do it with ddply
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果速度是主要考虑因素,您应该查看
data.table
包。当行数或分组列数很大时,data.table
看起来真的很闪耀。该包的 wiki 位于此处并且有几个链接其他好的介绍性文件。以下是使用
data.table()
进行聚合的方法。通过
比较 rbenchmark 包。然而,当 data.frame 包含 3e5 行时,
data.table()
成为明显的赢家。这是输出:If speed is a primary concern, you should take a look at the
data.table
package. When the number of rows or grouping columns is large,data.table
really seems to shine. The wiki for the package is here and has several links to other good introductory documents.Here's how you'd do this aggregation with
data.table()
Gives us
The aggregate solution provided above seems to fare pretty well when working with the 30 row data.frame by comparing the output from the rbenchmark package. However, when the data.frame contains 3e5 rows,
data.table()
pulls away as a clear winner. Here's the output:首先让我们定义一个玩具示例:
然后我们将
aggregate
与公式接口一起使用:First let's define a toy example:
And then we use
aggregate
with the formula interface: