Distance of user id

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

I think that you did not get an answer until now is due to the fact that your code is unreadable.
In my browser I see a strange symbol (a rectangle with a "v" in it) and variables user_id and userId.
Also it is not clear what is dist: it is probably meant to be the structure that you give.

In your function you do something with dist while dist is not an argument for this function.
You can do that in R but I would avoid that if not strictly necessary.

Also I could not find the distm function. If you use non-base functions please indicate the packages they belong to. And also formatting of your code does not look inviting. ...

Assuming that for each user you have timestamp and location records sorted by user and timestamp I would use package dplyr, group by user and use the lag function to get the 'previous' location. With that you can calculate the distance to the previous location.

Thanks for your response, I didn't know my code looked like that, but thanks to your suggestion, I was able to figure it out.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.