Hi guys,
Please help me. I have a data set and I am trying to calculate the distance each rider has risen. I tried to group the data by the user id and then calculate the distance for each. I tried use this code but it just kept giving me 0. The user id is the distance rider id.
dist <- data.frame(latitude, longitude,user_id)
distance_meters <- function(x) {
y <- x - 1
while (y > 0) {
y <- x - 1
if (isTRUE (dist$user_id[x ]<- dist$user_id[y]) && dist$userId[x ] == dist$userId[y]) {
meters <- distm(c(dist$longitude[ x ], dist$latitude[x ]), c(dist$longitude[y], dist$latitude[y]), fun = distHaversine)
return(meters[1, 1])
} else {
return(0)
}
}
if (y == 0) {
return(0)
}
if (y<0){
return(0)
}
}
tracking_df$distance_in_meters <- sapply(1:nrow(dist), distance_meters)
Here is a sample data set, let me know if I can send a sample dataset in a different way:
structure(list(X__v = c(0L, 0L, 0L, 0L, 0L, 0L), X_id = c("5e7774dd82c048341cba6e08",
"5e777c1a82c048341cba6e33", "5e777c3a82c048341cba6e37", "5e7b7bad82c048341cba6e7d",
"5e7cca9882c048341cba6ef9", "5e7cceb582c048341cba6f17"), coordinates = c("[-0.0733074,5.6120837]",
"[-0.0733074,5.6120837]", "[-0.0733282,5.6120518]", "[-0.216836,5.5558007]",
"[-0.2168314,5.5558838]", "[-0.2148753,5.5648851]"), latitude = c(5.6120837,
5.6120837, 5.6120518, 5.5558007, 5.5558838, 5.5648851), longitude = c(-0.0733074,
-0.0733074, -0.0733282, -0.216836, -0.2168314, -0.2148753), status = c(0L,
0L, 0L, 0L, 0L, 0L), time = c(1584887005424, 1584888858921, 1584888890218,
1585150893885, 1585236632509, 1585237685879), userId = c("5e77734182c048341cba6e04",
"5e77734182c048341cba6e04", "5e77734182c048341cba6e04", "5e7249c182c048341cba6dcc",
"5e7249c182c048341cba6dcc", "5e7249c182c048341cba6dcc"), Date_and_time = structure(c(1584887005.424,
1584888858.921, 1584888890.218, 1585150893.885, 1585236632.509,
1585237685.879), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
6L), class = "data.frame")