如何在 R 中聚合这些数据

发布于 2024-09-02 18:16:59 字数 896 浏览 3 评论 0原文

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

> 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 。因此,行将由 组成

unique(testData$date)

,列将由 组成

unique(testData$comm.code)

,每个单元格将由给定日期的所有 exch.codes 的总 oi 组成。

谢谢,

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

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)

and the columns would be

unique(testData$comm.code)

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

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜警司 2024-09-09 18:16:59

plyr 包擅长于此,您应该使用单个 ddply() 调用。像(未经测试)这样的东西

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

应该可以工作。

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))

should work.

贪恋 2024-09-09 18:16:59
# get it all aggregated
dfl <- aggregate(oi ~ date + comm.code, testData, sum)

# rearrange it so that it's like you requested
uc <- unique(df1$comm.code)
dfw <- with( df1, data.frame(data = unique(date), matrix(oi, ncol = length(uc))) )
names(dfw) <- c( 'date', uc)

这将比等效的 plyr 命令快得多。而且,有多种方法可以将其重新排列在一个衬垫中。重新排列部分非常快。

# get it all aggregated
dfl <- aggregate(oi ~ date + comm.code, testData, sum)

# rearrange it so that it's like you requested
uc <- unique(df1$comm.code)
dfw <- with( df1, data.frame(data = unique(date), matrix(oi, ncol = length(uc))) )
names(dfw) <- c( 'date', uc)

This will be much much faster than the equivalent plyr command. And, there are ways to rearrange it in one liners. The rearranging part is very fast.

data.table 解决方案

library(data.table)
DT <- data.table(testData)
DT[,sum(oi), by = list(date,comm.code)]

A data.table solution

library(data.table)
DT <- data.table(testData)
DT[,sum(oi), by = list(date,comm.code)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文