拆分、应用并将多个数据帧合并为一个数据帧 [英] Split, apply, and combine multiple data frames into one data frame

查看:28
本文介绍了拆分、应用并将多个数据帧合并为一个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个起点-终点成本矩阵(23 个起点,~600,000 个目的地),用于在 ArcGIS 中穿过街道网络,并使用 Python 脚本按商店 ID 将生成的矩阵分解为 DBF 表.我已将每个 DBF 表加载到 R 会话中,如下所示:

I have completed an origin-destination cost matrix (23 origins, ~600,000 destinations) for traveling through a street network in ArcGIS and disaggregated the resulting matrix into DBF tables by store ID using a Python script. I have loaded each DBF table into an R session as follows:

# Import OD cost matrix results for each store
origins <- read.dbf('ODM_origins.dbf')
store_17318 <- read.dbf('table_17318.dbf')
store_17358 <- read.dbf('table_17358.dbf')
store_17601 <- read.dbf('table_17601.dbf')
store_17771 <- read.dbf('table_17771.dbf')
store_18068 <- read.dbf('table_18068.dbf')
store_18261 <- read.dbf('table_18261.dbf')
store_18289 <- read.dbf('table_18289.dbf')
store_18329 <- read.dbf('table_18329.dbf')
store_18393 <- read.dbf('table_18393.dbf')
store_18503 <- read.dbf('table_18503.dbf')
store_18522 <- read.dbf('table_18522.dbf')
store_19325 <- read.dbf('table_19325.dbf')
store_19454 <- read.dbf('table_19454.dbf')
store_20068 <- read.dbf('table_20068.dbf')
store_20238 <- read.dbf('table_20238.dbf')
store_20292 <- read.dbf('table_20292.dbf')
store_20435 <- read.dbf('table_20435.dbf')
store_20465 <- read.dbf('table_20465.dbf')
store_20999 <- read.dbf('table_20999.dbf')
store_22686 <- read.dbf('table_22686.dbf')
store_22715 <- read.dbf('table_22715.dbf')
store_24445 <- read.dbf('table_24445.dbf')
store_24446 <- read.dbf('table_24446.dbf')
ID <- as.vector(origins$Name) # Create list of store IDs
object_list <- list(ls(pat="store_")) # Create list of DBF object names

这是每个数据框的布局:

Here's the layout of every data frame:

> head(store_17318)
  OID_          NAME ORIGINID DESTINATIO DESTINAT_1 TOTAL_TRAV SHAPE_LENG
1    0 17318 - 17318       25       5367          1  0.2056914   202.2393
2    0 17318 - 17318       25       5368          2  0.2056914   202.2393
3    0 17318 - 17318       25       5381          5  0.2432538   224.3947
4    0 17318 - 17318       25       5382          6  0.2432538   224.3947
5    0 17318 - 17318       25       5362          7  0.3670772   294.8987
6    0 17318 - 17318       25       5363          8  0.3670772   294.8987

对于每个数据框,我想通过商店 ID 找到旅行时间的汇总统计数据(平均值,SD)并将其写入新的数据框.这似乎是标准的拆分、应用、组合工作流程,但它涉及拆分多个对象.对此问题的任何帮助将不胜感激.

For every data frame, I would like to find the summary statistics (mean, SD) for travel time by store ID and write it to a new data frame. This seems like a standard split, apply, combine workflow but it involves splitting multiple objects. Any help with this problem would be appreciated.

推荐答案

你可以使用sapply:

res <- sapply(ls(pattern = "store_"), function(x) {
  tmp <- get(x)$TOTAL_TRAV
  c(mean = mean(tmp), SD = sd(tmp))
})

这将返回一个矩阵.列代表商店 ID.两行包含均值和标准差.

This will return a matrix. Columns represent store IDs. The two rows contain mean and standard deviation.

您可以使用

as.data.frame(t(res))

这里,两列包含均值和标准差.行名称代表商店 ID.

Here, the two columns contain mean and standard deviation. The row names represent store IDs.

这篇关于拆分、应用并将多个数据帧合并为一个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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