我有城镇(从A到D),它们有不同的人口,并且距离不同.目标是将居住在半径圆(XY距离)内的总人口加起来,其中X是圆圈中心的城镇,Y是任何其他城镇.
在这段代码中:
Df <- structure(list(Town_From = c("A", "A", "A", "B", "B", "C"), Town_To = c("B", "C", "D", "C", "D", "D"), Distance = c(10, 5, 18, 17, 20, 21)), .Names = c("Town_From", "Town_To", "Distance"), row.names = c(NA, -6L), class = "data.frame") Df2 <- structure(list(Town = c("A", "B", "C", "D"), Population = c(1000, 800, 500, 200)), .Names = c("Town", "Population"), row.names = c(NA, -4L), class = "data.frame") Df <- Df %>% left_join(Df2,by=c("Town_From"="Town")) %>% left_join(Df2,by=c("Town_To"="Town"))%>% group_by(Town_From) %>% arrange(Distance) colnames(Df)[4]<-c("pop_TF") colnames(Df)[5]<-c("pop_TT") Source: local data frame [6 x 5] Groups: Town_From [3] Town_From Town_To Distance pop_TF pop_TT1 A C 5 1000 500 2 A B 10 1000 800 3 B C 17 800 500 4 A D 18 1000 200 5 B D 20 800 200 6 C D 21 500 200
城镇由(Town_From)组织并由(距离)安排.
在5公里半径范围内(从A到C),活1000(A)+ 500(C)= 1500人; 在下一个圈内活1500 + 800(在B中)= 2300.在第三圈内仍有2300人,因为城镇A,B,C在圆半径B到C = 17公里内.在圆半径A至D = 18km内,直播2300 + 200(在D中)= 2500人.
这是所讨论的圆圈的可视化.从理论上讲,圆圈可以扩展到任意半径.在实践中,我只需要在城镇对之间的距离(计数变化的地方)检查它们.