简体   繁体   中英

Using geodist and ddply in R

I am trying to use the geodist function in the gmt package along with ddply in the plyr package to calculate the distance between two sets of coordinates by a grouping variable (Tow) but am getting this message:

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

The dataset is organized so that each row has a Tow, a Latitude and a Longitude and is ordered by Tow and Time. I would like to calculate the difference in position between consecutive rows within a Tow.

An example data set:

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")

Code:

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 information:

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 

In distance=geodist(tail(Lat-1), tail(Long-1),head(Lat,-1), head(Long,-1) are errors concerning comma.
One the one hand you write Lat, -1 and on the other hand Lat-1 .
I think your error comes from this.

J_F

Solution: Example: same as above but doesn't need whitespace scrubbing on the cut and paste.)

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")
          )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM