在R中使用geodist和ddply

时间:2016-06-16 15:43:08

标签: r plyr

我正在尝试使用gmt包中的geodist函数以及plyr包中的ddply,通过分组变量(Tow)计算两组坐标之间的距离,但是收到此消息:

Error: expecting result of length one, got : 8
In addition: Warning messages:
1: In N1 == N2 :
  longer object length is not a multiple of shorter object length
2: In E1 == E2 :
  longer object length is not a multiple of shorter object length

对数据集进行组织,使每行具有Tow,Latitude和Longitude,并按Tow和Time排序。我想计算Tow内连续行之间的位置差异。

示例数据集:

example <- structure(list(Tow = c(201602001L, 201602001L, 201602001L, 201602001L, 201602001L, 201602001L, 201602001L, 201602001L, 201602001L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L, 201602002L), Time = c("21:57:41", "21:58:30", "21:59:18", "22:00:06", "22:00:55", "22:07:14", "22:08:30", "22:09:46", "22:11:21", "23:10:40", "23:11:25", "23:12:11", "23:12:11", "23:12:55", "23:13:41", "23:14:27", "23:15:14", "23:16:00", "23:16:46", "23:17:32", "23:18:16", "23:19:02", "23:19:48", "23:20:34", "23:21:17"),     Long = c(-74.579651, -74.579631, -74.579754, -74.579885,     -74.580056, -74.579971, -74.580008, -74.580114, -74.580359,     -76.261671, -76.260058, -76.258475, -76.258475, -76.256906,     -76.255294, -76.253698, -76.252086, -76.25054, -76.248975,     -76.247434, -76.245858, -76.244288, -76.242696, -76.241143,     -76.239586), Lat = c(37.423733, 37.425438, 37.427116, 37.428796,     37.430466, 37.432135, 37.433818, 37.435483, 37.437141, 37.184116,     37.183005, 37.181895, 37.181895, 37.180741, 37.179638, 37.178505,     37.177406, 37.176231, 37.175095, 37.173918, 37.172818, 37.171664,     37.170543, 37.169419, 37.168305), Date = structure(c(2L,     2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2016/05/04",     "2016/05/05", "2016/05/06"), class = "factor"), TowStartDate = structure(c(1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400), class = c("POSIXct",     "POSIXt"), tzone = ""), TowEndDate = structure(c(1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400, 1462334400,     1462334400, 1462334400, 1462334400, 1462334400), class = c("POSIXct",     "POSIXt"), tzone = ""), InclinometerStart = c("21:57:40",     "21:57:40", "21:57:40", "21:57:40", "21:57:40", "21:57:40",     "21:57:40", "21:57:40", "21:57:40", "23:10:20", "23:10:20",     "23:10:20", "23:10:20", "23:10:20", "23:10:20", "23:10:20",     "23:10:20", "23:10:20", "23:10:20", "23:10:20", "23:10:20",     "23:10:20", "23:10:20", "23:10:20", "23:10:20"), InclinometerEnd = c("22:12:50",     "22:12:50", "22:12:50", "22:12:50", "22:12:50", "22:12:50",     "22:12:50", "22:12:50", "22:12:50", "23:26:10", "23:26:10",     "23:26:10", "23:26:10", "23:26:10", "23:26:10", "23:26:10",     "23:26:10", "23:26:10", "23:26:10", "23:26:10", "23:26:10",     "23:26:10", "23:26:10", "23:26:10", "23:26:10"), test = c(1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1)), .Names = c("Tow", "Time", "Long", "Lat", "Date", "TowStartDate", "TowEndDate", "InclinometerStart", "InclinometerEnd", "test"), row.names = c(760L, 369L, 429L, 153L, 1467L, 949L, 1286L, 1080L, 1017L, 1893L, 3518L, 3041L, 3402L, 2803L, 2784L, 2897L, 3133L, 3000L, 3084L, 2723L, 3177L, 3572L, 2539L, 2768L, 2709L), class = "data.frame")

代码:

library(plyr)
library(gmt)

new<-ddply(example, .(Tow), summarize, distance=geodist(tail(Lat-1), tail(Long-1),head(Lat,-1), head(Long,-1), units="km"))

R信息:

R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3     plyr_1.8.3      gmt_1.2-0       RODBC_1.3-12   
[5] lubridate_1.5.0 rgdal_1.0-6     maptools_0.8-36 sp_1.1-1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6     lattice_0.20-31 assertthat_0.1  R6_2.1.0       
 [5] grid_3.2.1      DBI_0.3.1       magrittr_1.5    stringi_0.5-5  
 [9] lazyeval_0.1.10 tools_3.2.1     stringr_1.0.0   foreign_0.8-63 
[13] parallel_3.2.1 

1 个答案:

答案 0 :(得分:1)

distance=geodist(tail(Lat-1), tail(Long-1),head(Lat,-1), head(Long,-1)中有关于逗号的错误 一方面,你写Lat, -1,另一方面Lat-1 我认为你的错误来自于此。

J_F

解决方案: 示例:与上述相同,但不需要在剪切和粘贴上进行空白划线。)

library(plyr)
library(gmt)

## example as defined above
new<-ddply(example, .(Tow), summarize,
           distance=geodist(tail(Lat,-1), tail(Long,-1),
           head(Lat,-1), head(Long,-1), units="km")
          )