You may find the dplyr
to provide the tools you need to do this.
First, if you plan on posting more questions on SO, it will be helpful to use dput
to provide copies of your data set in a way that is easy for others to reproduce. For this example, it was easy enough to read your data set in via readr
.
library(readr)
library(dplyr)
library(trip)
p.1 <-
readr::read_delim(delim = "|",
locale = locale(decimal_mark = ",", grouping_mark = "."),
trim_ws = TRUE,
file =
"date | lon | lat | trip | distancetocoast
2014-06-11 19:02:00 | -58,3508585 | -51,88438373 | 1 | 2236,067977
2014-06-12 01:02:00 | -58,35589725 | -51,88349529 | 1 | 1000
2014-06-12 13:02:00 | -58,27224941 | -51,91903677 | 1 | 7211,102551
2014-06-12 19:02:00 | -58,27974654 | -51,90535003 | 1 | 5830,951895
2014-06-13 01:02:00 | -58,32331901 | -51,89410464 | 1 | 3605,551275
2014-06-13 07:02:00 | -58,35833139 | -51,88809227 | 1 | 1414,213562
2014-06-13 13:02:00 | -58,35617673 | -51,88156281 | 1 | 1000
2014-06-13 19:02:00 | -58,34055711 | -51,89002367 | 1 | 2236,067977
2014-06-14 01:02:00 | -58,34982536 | -51,8715761 | 2 | 1000
2014-06-14 13:02:00 | -58,3073814 | -51,92722937 | 2 | 7071,067812
2014-06-14 19:02:00 | -58,34581314 | -51,86761133 | 3 | 1000
2014-06-15 01:02:00 | -58,34050624 | -51,88382088 | 3 | 1414,213562
2014-06-15 13:02:00 | -58,2974691 | -51,91795326 | 3 | 6324,55532
2014-06-15 19:02:00 | -58,19881901 | -51,95172233 | 3 | 13000
2014-06-16 01:02:00 | -58,1348416 | -51,98673766 | 3 | 18788,29423
2014-06-16 07:02:00 | -57,99399544 | -52,06988191 | 3 | 28861,73938
2014-06-16 13:02:00 | -58,00469754 | -52,02795069 | 3 | 26627,05391
2014-06-16 19:02:00 | -57,92758675 | -52,02184666 | 3 | 29000
2014-06-17 01:02:00 | -57,91658235 | -51,99748699 | 3 | 28284,27125
2014-06-17 07:02:00 | -57,77015528 | -51,99031797 | 3 | 30805,8436
2014-06-17 13:02:00 | -57,99601712 | -51,91519551 | 3 | 17804,49381
2014-06-17 19:02:00 | -58,06820013 | -51,92972737 | 3 | 14866,06875
2014-06-18 01:02:00 | -58,19845185 | -51,89522513 | 3 | 7615,773106
2014-06-18 07:02:00 | -58,35241361 | -51,88015998 | 3 | 1000
2014-06-18 13:02:00 | -58,35603546 | -51,88336878 | 3 | 1000
2014-06-18 19:02:00 | -58,33350332 | -51,87308427 | 3 | 1000
2014-06-19 01:02:00 | -58,33839581 | -51,87846631 | 3 | 1414,213562
2014-06-19 07:02:00 | -58,42661519 | -51,80902388 | 4 | 0
2014-06-19 13:02:00 | -58,30461883 | -51,93745837 | 4 | 7810,249676
2014-06-19 19:02:00 | -58,18362875 | -51,96475914 | 4 | 14317,82106")
dplyr
provides a group_by
function that will do exactly what it sounds like, create groups defined by the values within one or more columns. The mutate
command creates columns of a data set.
A vector distance_traveled
has been created to start at 0 for the first entry of each trip
and then use spDist
for the distance traveled.
The cumulative distance and the total distance for each trip
is also reported. If you are unfamiliar with the %>%
operator, read help("%>%", package = "magrittr")
.
p.1 %>%
group_by(trip) %>%
arrange(date) %>%
mutate(distance_traveled = c(0, spDists(x = cbind(lon, lat), longlat = TRUE, segments = TRUE)),
cumlative_trip_distance = cumsum(distance_traveled),
total_trip_distance = sum(distance_traveled)) %>%
ungroup() %>%
print.data.frame
p.1
# date lon lat trip distancetocoast distance_traveled cumlative_trip_distance total_trip_distance
# 1 2014-06-11 19:02:00 -58.35086 -51.88438 1 2236.068 0.0000000 0.0000000 16.875313
# 2 2014-06-12 01:02:00 -58.35590 -51.88350 1 1000.000 0.3607526 0.3607526 16.875313
# 3 2014-06-12 13:02:00 -58.27225 -51.91904 1 7211.103 6.9846980 7.3454506 16.875313
# 4 2014-06-12 19:02:00 -58.27975 -51.90535 1 5830.952 1.6078870 8.9533376 16.875313
# 5 2014-06-13 01:02:00 -58.32332 -51.89410 1 3605.551 3.2496828 12.2030205 16.875313
# 6 2014-06-13 07:02:00 -58.35833 -51.88809 1 1414.214 2.5015138 14.7045343 16.875313
# 7 2014-06-13 13:02:00 -58.35618 -51.88156 1 1000.000 0.7415006 15.4460348 16.875313
# 8 2014-06-13 19:02:00 -58.34056 -51.89002 1 2236.068 1.4292778 16.8753126 16.875313
# 9 2014-06-14 01:02:00 -58.34983 -51.87158 2 1000.000 0.0000000 0.0000000 6.846905
# 10 2014-06-14 13:02:00 -58.30738 -51.92723 2 7071.068 6.8469049 6.8469049 6.846905
# 11 2014-06-14 19:02:00 -58.34581 -51.86761 3 1000.000 0.0000000 0.0000000 103.020176
# 12 2014-06-15 01:02:00 -58.34051 -51.88382 3 1414.214 1.8402281 1.8402281 103.020176
# 13 2014-06-15 13:02:00 -58.29747 -51.91795 3 6324.555 4.8164202 6.6566483 103.020176
# 14 2014-06-15 19:02:00 -58.19882 -51.95172 3 13000.000 7.7558235 14.4124718 103.020176
# 15 2014-06-16 01:02:00 -58.13484 -51.98674 3 18788.294 5.8746536 20.2871254 103.020176
# 16 2014-06-16 07:02:00 -57.99400 -52.06988 3 28861.739 13.3804466 33.6675720 103.020176
# 17 2014-06-16 13:02:00 -58.00470 -52.02795 3 26627.054 4.7230685 38.3906405 103.020176
# 18 2014-06-16 19:02:00 -57.92759 -52.02185 3 29000.000 5.3362603 43.7269008 103.020176
# 19 2014-06-17 01:02:00 -57.91658 -51.99749 3 28284.271 2.8138115 46.5407123 103.020176
# 20 2014-06-17 07:02:00 -57.77016 -51.99032 3 30805.844 10.0892378 56.6299501 103.020176
# 21 2014-06-17 13:02:00 -57.99602 -51.91520 3 17804.494 17.6348017 74.2647518 103.020176
# 22 2014-06-17 19:02:00 -58.06820 -51.92973 3 14866.069 5.2225348 79.4872866 103.020176
# 23 2014-06-18 01:02:00 -58.19845 -51.89523 3 7615.773 9.7503783 89.2376649 103.020176
# 24 2014-06-18 07:02:00 -58.35241 -51.88016 3 1000.000 10.7319100 99.9695749 103.020176
# 25 2014-06-18 13:02:00 -58.35604 -51.88337 3 1000.000 0.4355097 100.4050846 103.020176
# 26 2014-06-18 19:02:00 -58.33350 -51.87308 3 1000.000 1.9279737 102.3330583 103.020176
# 27 2014-06-19 01:02:00 -58.33840 -51.87847 3 1414.214 0.6871181 103.0201763 103.020176
# 28 2014-06-19 07:02:00 -58.42662 -51.80902 4 0.000 0.0000000 0.0000000 25.433053
# 29 2014-06-19 13:02:00 -58.30462 -51.93746 4 7810.250 16.5773793 16.5773793 25.433053
# 30 2014-06-19 19:02:00 -58.18363 -51.96476 4 14317.821 8.8556734 25.4330527 25.433053