我在尝试着dcast
大型数据集(数百万行)。我有一行用于到达时间和出发地,另一行用于出发时间和目的地。有一个id
识别这两种情况下的单位。它看起来类似于:
id time movement origin dest
1 10/06/2011 15:54 ARR 15 15
1 10/06/2011 16:14 DEP 15 29
2 10/06/2011 17:59 ARR 73 73
2 10/06/2011 18:10 DEP 73 75
2 10/06/2011 21:10 ARR 75 75
2 10/06/2011 21:20 DEP 75 73
3 10/06/2011 17:14 ARR 17 17
3 10/06/2011 18:01 DEP 17 48
4 10/06/2011 17:14 ARR 49 49
4 10/06/2011 17:26 DEP 49 15
所以,我想重新分配这些对(ARR
-DEP
)并有效地做到这一点(如here https://stackoverflow.com/questions/27552810/optimizing-for-loop-in-big-data-frame)。由于它是一个非常大的数据集for loop
在这种情况下不起作用。理想的输出是
index unitid origin arr time dest dep time
1 1 15 10/06/2011 14:33 29 10/06/2011 19:24
2 2 73 10/06/2011 14:59 75 10/06/2011 17:23
3 2 75 10/06/2011 21:10 73 10/06/2011 23:40
Data:
df <- structure(list(time = structure(c(7L, 16L, 8L, 11L, 18L, 20L,
10L, 12L, 3L, 6L, 15L, 19L, 9L, 4L, 5L, 14L, 1L, 2L, 13L, 17L
), .Label = c("10/06/2011 09:08", "10/06/2011 10:54", "10/06/2011 11:38",
"10/06/2011 12:41", "10/06/2011 12:54", "10/06/2011 14:26", "10/06/2011 14:33",
"10/06/2011 14:59", "10/06/2011 17:12", "10/06/2011 17:14", "10/06/2011 17:23",
"10/06/2011 18:56", "10/06/2011 19:03", "10/06/2011 19:04", "10/06/2011 19:16",
"10/06/2011 19:24", "10/06/2011 20:12", "10/06/2011 21:10", "10/06/2011 22:28",
"10/06/2011 23:40"), class = "factor"), movement = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 2L, 1L,
2L, 2L, 3L), .Label = c("ARR", "DEP", "ITZ"), class = "factor"),
origin = c(15L, 15L, 73L, 73L, 75L, 75L, 17L, 17L, 49L, 49L,
15L, 15L, 32L, 10L, 10L, 17L, 76L, 76L, 76L, 76L), dest = c(15L,
29L, 73L, 75L, 75L, 73L, 17L, 48L, 49L, 15L, 15L, 49L, 32L,
10L, 17L, 10L, 76L, 65L, 76L, 65L), id = c(1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L, 7L, 7L, 8L,
8L)), .Names = c("time", "movement", "origin", "dest", "id"
), row.names = c(NA, -20L), class = "data.frame")