在小组结尾附加观察

时间:2016-06-08 09:11:04

标签: r dataframe grouping

我在test中有一个由route_id分组的纬度/经度列表。我的目标是在单独的数据框obs中将观察结果附加到每个组的末尾 - 匹配列为route_id。怎么会在R中这样做呢?

> test
   route_id start_lon start_lat
1         2   37.3387   55.7278
2         2   37.3377   55.7281
3         2   37.3358   55.7285
4         2   37.3359   55.7286
5         2   37.3449   55.7356
6         2   37.3394   55.7403
7         3   37.3387   55.7278
8         3   37.3377   55.7281
9         3   37.3321   55.7293
10        3   37.3360   55.7346
11        3   37.3309   55.7451
12        6   37.3387   55.7278
13        6   37.3377   55.7281
14        6   37.3384   55.7324
15        6   37.3389   55.7343
16        6   37.3521   55.7294
17        6   37.3537   55.7233
18        6   37.3722   55.7237
19        6   37.3734   55.7235
20        6   37.3999   55.7424
21        6   37.3992   55.7432
22        6   37.3565   55.7765
23        6   37.3564   55.7761
24        6   37.3555   55.7765
25        6   37.3514   55.7761
26        6   37.3500   55.7766
27        6   37.3447   55.7726


> obs
   route_id end_lon end_lat
6         2 37.3382 55.7401
11        3 37.3313 55.7463
27        6 37.3389 55.7691


dput(test)
structure(list(route_id = c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L), start_lon = c(37.3387, 37.3377, 37.3358, 37.3359, 37.3449, 
37.3394, 37.3387, 37.3377, 37.3321, 37.336, 37.3309, 37.3387, 
37.3377, 37.3384, 37.3389, 37.3521, 37.3537, 37.3722, 37.3734, 
37.3999, 37.3992, 37.3565, 37.3564, 37.3555, 37.3514, 37.35, 
37.3447), start_lat = c(55.7278, 55.7281, 55.7285, 55.7286, 55.7356, 
55.7403, 55.7278, 55.7281, 55.7293, 55.7346, 55.7451, 55.7278, 
55.7281, 55.7324, 55.7343, 55.7294, 55.7233, 55.7237, 55.7235, 
55.7424, 55.7432, 55.7765, 55.7761, 55.7765, 55.7761, 55.7766, 
55.7726)), .Names = c("route_id", "start_lon", "start_lat"), row.names = c(NA, 
27L), class = "data.frame")

dput(obs)
structure(list(route_id = c(2L, 3L, 6L), end_lon = c(37.3382, 
37.3313, 37.3389), end_lat = c(55.7401, 55.7463, 55.7691)), .Names = c("route_id", 
"end_lon", "end_lat"), row.names = c(6L, 11L, 27L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

我们可以将它们放在list中,与rbindlist绑定,然后在' route_id'上执行order

library(data.table)
rbindlist(list(test, obs))[order(route_id)]
#   route_id start_lon start_lat
# 1:        2   37.3387   55.7278
# 2:        2   37.3377   55.7281
# 3:        2   37.3358   55.7285
# 4:        2   37.3359   55.7286
# 5:        2   37.3449   55.7356
# 6:        2   37.3394   55.7403
# 7:        2   37.3382   55.7401
# 8:        3   37.3387   55.7278
# 9:        3   37.3377   55.7281
#10:        3   37.3321   55.7293
#11:        3   37.3360   55.7346
#12:        3   37.3309   55.7451
#13:        3   37.3313   55.7463
#14:        6   37.3387   55.7278
#15:        6   37.3377   55.7281
#16:        6   37.3384   55.7324
#17:        6   37.3389   55.7343
#18:        6   37.3521   55.7294
#19:        6   37.3537   55.7233
#20:        6   37.3722   55.7237
#21:        6   37.3734   55.7235
#22:        6   37.3999   55.7424
#23:        6   37.3992   55.7432
#24:        6   37.3565   55.7765
#25:        6   37.3564   55.7761
#26:        6   37.3555   55.7765
#27:        6   37.3514   55.7761
#28:        6   37.3500   55.7766
#29:        6   37.3447   55.7726
#30:        6   37.3389   55.7691