为非常大的数据集生成 smmary 表

Producing smmary tables for very large datasets

我正在处理迁移数据,我想从一个非常大的数据集(>400 万)中生成三个摘要 table。详细示例如下:

migration <- structure(list(area.old = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
                                                   2L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("leeds", 
                                                                                                                   "london", "plymouth"), class = "factor"), area.new = structure(c(7L, 
                                                                                                                                                                                    13L, 3L, 2L, 4L, 7L, 6L, 7L, 6L, 13L, 5L, 8L, 7L, 11L, 12L, 9L, 
                                                                                                                                                                                    1L, 10L, 11L), .Label = c("bath", "bristol", "cambridge", "glasgow", 
                                                                                                                                                                                                              "harrogate", "leeds", "london", "manchester", "newcastle", "oxford", 
                                                                                                                                                                                                              "plymouth", "poole", "york"), class = "factor"), persons = c(6L, 
                                                                                                                                                                                                                                                                           3L, 2L, 5L, 6L, 7L, 8L, 4L, 5L, 6L, 3L, 4L, 1L, 1L, 2L, 3L, 4L, 
                                                                                                                                                                                                                                                                           9L, 4L)), .Names = c("area.old", "area.new", "persons"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                        -19L))

总结table1:'area.within'

我想创建的第一个 table 叫做 'area.within'。这将仅详细说明人们在同一区域内移动的区域(即,它将计算在 'area.old' 和 'area.new' 中记下 'london' 的总人数)。在数据 table 中可能会多次出现这种情况。然后它将针对所有不同区域执行此操作,因此摘要将是:

      area.within persons
1      london      13
2       leeds       5
3    plymouth       5

使用数据table包,我有:

setDT(migration)[as.character(area.old)==as.character(area.new)]

...但这并没有消除重复项...

总结table2:'moved.from'

第二个 table 将总结人们搬出的区域(即 'area.old' 中的那些独特值)。它将识别第 1 列和第 2 列不同的区域,并将所有详细的人加在一起(即排除那些在区域之间移动的人 - 总结 table 1)。结果 table 应该是:

      moved.from persons
1     london      24
2      leeds      17
3   plymouth      19

总结table3:'moved.to'

第三个 table 总结了哪些地区经历过人们的迁移(即 'area.new' 中的那些独特值)。它将识别第 1 列和第 2 列不同的所有独特区域,并将所有详细的人加在一起(即排除那些在区域之间移动的人 - 总结 table 1)。结果 table 应该是:

     moved.to persons
1      london       5
2        york       3
3   cambridge       2
4     bristol       5
5     glasgow       6
6       leeds       8
7        york       6
8   harrogate       3
9  manchester       4
10   plymouth       0
11      poole       2
12  newcastle       3
13       bath       4
14     oxford       9

最重要的是,tables 2 和 3 中详述的所有人员的总和应该相同。然后这个值加上 table 1 的总人数应该等于原始 table.

中所有人的总和

如果有人能帮我弄清楚如何使用数据 table 包构建我的代码来生成我的 tables,我将不胜感激。

我觉得用data.table是个不错的选择。

setDT(migration) #This has to be done only once

1.

为避免重复,只需按城市汇总如下

migration[as.character(area.old)==as.character(area.new), 
                 .(persons = sum(persons)), 
                 by=.(area.within = area.new)]

2.

这与 1. 非常相似,但在 i-Argument

中使用 !=
migration[as.character(area.old)!=as.character(area.new), 
                 .(persons = sum(persons)), 
                 by=.(moved.from = area.old)]

3.

同 2.

migration[as.character(area.old)!=as.character(area.new), 
                 .(persons = sum(persons)), 
                 by=.(moved.to = area.new)]

备选 由于 2. 和 3. 非常相似,您还可以这样做:

moved <- migration[as.character(area.old)!=as.character(area.new)]
#2
moved[,.(persons = sum(persons)), by=.(moved.from = area.old)]
#3
moved[,.(persons = sum(persons)), by=.(moved.to = area.new)]

因此只需选择正确的行一次。