如何在R中汇总此数据 [英] how to aggregate this data in R

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

问题描述

我在R中有一个具有以下结构的数据框。

I have a data frame in R with the following structure.

> testData
            date exch.code comm.code     oi
1     1997-12-30       CBT         1 468710
2     1997-12-23       CBT         1 457165
3     1997-12-19       CBT         1 461520
4     1997-12-16       CBT         1 444190
5     1997-12-09       CBT         1 446190
6     1997-12-02       CBT         1 443085
....
    77827 2004-10-26      NYME       967  10038
    77828 2004-10-19      NYME       967   9910
    77829 2004-10-12      NYME       967  10195
    77830 2004-09-28      NYME       967   9970
    77831 2004-08-31      NYME       967   9155
    77832 2004-08-24      NYME       967   8655

我想做的是生成一张表格,显示指定日期的商品,商品在每个交换代码中的总oi。因此,这些行将由

What I want to do is produce a table the shows for a given date and commodity the total oi across every exchange code. So, the rows would be made up of

unique(testData$date)

,列将是

unique(testData$comm.code)

每个单元格将是给定所有exch.code上的oi总数

and each cell would be the total oi over all exch.codes on a given day.

谢谢

推荐答案

plyr 软件包很擅长此操作,您应该使用单个 ddply( )调用。像(未测试)

The plyr package is good at this, and you should get this done with a single ddply() call. Something like (untested)

ddply(testData, .(date,comm.code), function(x) sum(x$oi))

应该可以。

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

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