如何快速汇总和汇总数据? [英] How does one aggregate and summarize data quickly?

查看:17
本文介绍了如何快速汇总和汇总数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标题如下所示的数据集:

I have a dataset whose headers look like so:

PID Time Site Rep Count

我想通过 Rep 对每个 PID x Time x Site 组合的 Count 求和

I want sum the Count by Rep for each PID x Time x Site combo

在生成的 data.frame 上,我想获取 PID x Time x Site 组合的 Count 的平均值.

on the resulting data.frame, I want to get the mean value of Count for PID x Time x Site combo.

目前的功能如下:

dummy <- function (data)
{
A<-aggregate(Count~PID+Time+Site+Rep,data=data,function(x){sum(na.omit(x))})
B<-aggregate(Count~PID+Time+Site,data=A,mean)
return (B)
}

这非常慢(原始 data.frame 是 510000 20).有没有办法用 plyr 加快速度?

This is painfully slow (original data.frame is 510000 20). Is there a way to speed this up with plyr?

推荐答案

您应该查看包 data.table 以更快地对大型数据帧进行聚合操作.对于您的问题,解决方案如下所示:

You should look at the package data.table for faster aggregation operations on large data frames. For your problem, the solution would look like:

library(data.table)
data_t = data.table(data_tab)
ans = data_t[,list(A = sum(count), B = mean(count)), by = 'PID,Time,Site']

这篇关于如何快速汇总和汇总数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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