I have two datasets of trip durations and parking durations, and I want to put distance travelled of trips in parking durations in the way which every distance travelled would go to in rows of parking durations that is going to happen after trip as shown in picture. I would like to this task do in R, but I am lacking some skills to make that happen. Any suggestions? Any help would be appreciated!
Asked
Active
Viewed 26 times
-2
-
Hi Asrorkhuja Ortikov. It's not toally clear what you want to do. If you want to improve your question, here is some information on [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). The MRE will make it easier for others to find and test a answer to your question. That way you can help others to help you! – dario Mar 09 '20 at 19:09
-
I think what they want to do is match trip end with parking start. I.e. they park after they have finished their trip and they want to see the distance they traveled before parking. – Jordan Hackett Mar 09 '20 at 19:13
-
@dario I have edited my question, hopefully it's more clear now. – Asrorkhuja Ortikov Mar 09 '20 at 19:22
-
@JordanHackett exactly – Asrorkhuja Ortikov Mar 09 '20 at 19:23
-
@AsrorkhujaOrtikov please check the links provided, which would have mentioned the edit should have contained parts of the data. Just changing wording is not enough to make a minimal reproducible example. – Annet Mar 09 '20 at 19:28
-
@Annet, now I understood. Will improve next time for sure, so far code is working perfectly – Asrorkhuja Ortikov Mar 11 '20 at 07:31
1 Answers
2
The way you are phrasing your question at this moment it looks like you want a simple join. Using the tidyverse
package:
library(tidyverse)
trip <- tibble(ID= c("car1","car1","car2","car2"),
start_trip = c("6:30","16:15", "07:05","15:30"),
end_trip = c("7:00","16:45","07:40","16:05"),
trip_distance = c(20,30,25,20))
parking <- tibble(ID= c("car1","car1","car1","car2","car2","car2"),
end = c("6:30","16:15","0:00", "07:05","15:30","0:00"),
start = c("0:00","7:00","16:45","0:00","07:40","16:05"))
parking %>% left_join(trip %>% select(-start_trip), by = c("start" = "end_trip", "ID"))
which will result in
ID end start trip_distance
<chr> <chr> <chr> <dbl>
1 car1 6:30 0:00 NA
2 car1 16:15 7:00 20
3 car1 0:00 16:45 30
4 car2 07:05 0:00 NA
5 car2 15:30 07:40 25
6 car2 0:00 16:05 20
So I match the times from the trip dataframe to the times of the parking dataframe (assuming the end of a trip results in exactly the same parking time!). Also, the Car ID has to match. IF there is no match the distance will be NA. I removed the start_trip from the trip dataframe as you did not give that one in your example.

Annet
- 846
- 3
- 14