Split, apply, and combine multiple data frames into one data frame

半城伤御伤魂 提交于 2021-02-07 20:44:42

问题


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

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.


回答1:


You can use sapply:

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

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

You can transform this matrix to a (transposed) data frame with

as.data.frame(t(res))

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



来源:https://stackoverflow.com/questions/20791279/split-apply-and-combine-multiple-data-frames-into-one-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!