I have a data set that looks like this:
structure(list(Date2 = structure(c(18428, 18438, 18428, 18438,
18428, 18438, 18428, 18438, 18428, 18438, 18428, 18438, 18428,
18438, 18428, 18438, 18428, 18438, 18428, 18438, 18428, 18438,
18428, 18438, 18428, 18438, 18428, 18438, 18428, 18438, 18428,
18438, 18428, 18438, 18428, 18438, 18428, 18438, 18428, 18438,
18428, 18438, 18428, 18438), class = "Date"), Fish_ID = c("Fork1",
"Fork1", "Fork10", "Fork10", "Fork12", "Fork12", "Fork13", "Fork13",
"Fork14", "Fork14", "Fork15", "Fork15", "Fork16", "Fork16", "Fork17",
"Fork17", "Fork18", "Fork18", "Fork19", "Fork19", "Fork2", "Fork2",
"Fork20", "Fork20", "Fork21", "Fork21", "Fork22", "Fork22", "Fork23",
"Fork23", "Fork3", "Fork3", "Fork4", "Fork4", "Fork5", "Fork5",
"Fork6", "Fork6", "Fork7", "Fork7", "Fork8", "Fork8", "Fork9",
"Fork9"), Lat2 = c(32.9394, NA, 32.92935, NA, NA, 32.9047333333333,
NA, 32.9093833333333, NA, 32.9509833333333, 32.9160666666667,
NA, NA, 32.9074333333333, NA, 32.9029, NA, 32.90775, NA, 32.9094,
NA, NA, 32.9455166666667, 32.9459166666667, 32.9431, 32.9437666666667,
32.90365, 32.9044333333333, 32.9056166666667, 32.90585, NA, 32.9475333333333,
32.94325, NA, 32.9288833333333, NA, NA, NA, 32.9297, NA, NA,
NA, 32.9303, NA), Long2 = c(-95.6334, NA, -95.6406, NA, NA, -95.6531666666667,
NA, -95.6486, NA, -95.6252333333333, -95.648, NA, NA, -95.6391166666667,
NA, -95.64155, NA, -95.6393666666667, NA, -95.63895, NA, NA,
-95.6391166666667, -95.6389333333333, -95.6365, -95.6401333333333,
-95.6535666666667, -95.6532833333333, -95.6560333333333, -95.6575166666667,
NA, -95.63015, -95.6334333333333, NA, -95.6395, NA, NA, NA, -95.6398833333333,
NA, NA, NA, -95.6425166666667, NA), lag.Lat2 = c(NA, 32.9394,
NA, 32.92935, NA, NA, NA, NA, NA, NA, NA, 32.9160666666667, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 32.9455166666667, NA,
32.9431, NA, 32.90365, NA, 32.9056166666667, NA, NA, NA, 32.94325,
NA, 32.9288833333333, NA, NA, NA, 32.9297, NA, NA, NA, 32.9303
), lag.Long2 = c(NA, -95.6334, NA, -95.6406, NA, NA, NA, NA,
NA, NA, NA, -95.648, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, -95.6391166666667, NA, -95.6365, NA, -95.6535666666667, NA,
-95.6560333333333, NA, NA, NA, -95.6334333333333, NA, -95.6395,
NA, NA, NA, -95.6398833333333, NA, NA, NA, -95.6425166666667)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -44L), groups = structure(list(
Fish_ID = c("Fork1", "Fork10", "Fork12", "Fork13", "Fork14",
"Fork15", "Fork16", "Fork17", "Fork18", "Fork19", "Fork2",
"Fork20", "Fork21", "Fork22", "Fork23", "Fork3", "Fork4",
"Fork5", "Fork6", "Fork7", "Fork8", "Fork9"), .rows = structure(list(
1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18,
19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34,
35:36, 37:38, 39:40, 41:42, 43:44), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -22L), .drop = TRUE))
Lat2 and Long2 are the locations of each fish and lag values are for the location of each fish the prior time they were located.
I am trying to calculate the distance between each Long2
Lat2
value and each lag.Long2
lag.Long2
value so that I can calculate the distance traveled from the last time each fish was located. I know how to do this by hand for each one using the geosphere package, but I'm wondering if there is a for loop I could write to do this calculation for each individual fish so I could automate the process?
Thanks!
CodePudding user response:
using your data, we're looking for fish that moved, fishx and fishy, as matrix, with long before lat, so perhaps:
complete <- which(complete.cases(fish_df)==TRUE)
fishx <- as.matrix(fish_df[complete, 4:3])
fishx
Long2 Lat2
24 -95.63893 32.94592
26 -95.64013 32.94377
28 -95.65328 32.90443
30 -95.65752 32.90585
fishy <- as.matrix(fish_df[complete, 6:5])
geosphere::distm(fishx, fishy, fun = geosphere::distGeo)
[,1] [,2] [,3] [,4]
[1,] 47.55876 386.4671 4883.23872 4746.9509
[2,] 216.11520 347.7147 4623.08009 4484.7115
[3,] 4745.03178 4566.5523 90.82777 288.8099
[4,] 4723.80758 4574.9848 442.81633 141.1616
diag(geosphere::distm(fishx, fishy, fun = geosphere::distGeo))
[1] 47.55876 347.71468 90.82777 141.16164
# presumably in meters
You know your study area so this may or may not be correct... Which is to say, no need for loop, just get your matrices.