My data consists of distances and times traveled for dollar bills. My data looks like this:
bid ts latitude longitude
1 123 0 38.40513 41.83777
2 123 23 38.41180 41.68493
3 123 45 42.20771 43.36318
4 123 50 40.22803 43.00208
5 456 0 39.12882 42.73877
6 456 12 38.46078 42.79847
7 456 27 40.53698 42.57617
8 456 19 39.04038 42.17070
9 234 0 39.18274 41.17445
10 234 8 39.58652 43.61317
11 234 15 41.32383 41.49377
12 234 23 40.26008 42.01927
bid = bill id
ts = time stamp (days) calculated from original data point when t = 0
latitude and longitude = location
This data shows the movements for bill id's around the United States.
I want to calculate the difference in squared distance and time between all possible combinations of each like row group of 4. For example, for the group of bid's 123 I want to calculate the differences in distance and time between: row 1 and row 2, row 1 and row 3, row 1 and row 4, row 2 and row 3, row 2 and row 4, row 3 and row 4.
This would give me all possible combinations of calculations between this grouping of bid's.
I was able to do this with dplyr between successive rows like this:
detach("package:plyr", unload=TRUE)
library(magrittr)
library(dplyr)
library(geosphere)
deltadata <- group_by(df, bid) %>%
mutate(
dsq = (c(NA,distHaversine(cbind(longitude[-n()], latitude[-n()]),
cbind(longitude[ -1], latitude[ -1]))))^2,
dt = c(NA, diff(ts))
)%>%
ungroup() %>%
filter( ! is.na(dsq) )
deltadata
# A tibble: 21 x 6
bid ts latitude longitude dsq dt
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 123 23 38.41180 41.68493 178299634 23
2 123 45 42.20771 43.36318 198827672092 22
3 123 50 40.22803 43.00208 49480260636 5
4 456 12 38.46078 42.79847 5557152213 12
5 456 27 40.53698 42.57617 53781504422 15
6 456 19 39.04038 42.17070 28958550947 -8
7 234 8 39.58652 43.61317 46044153364 8
8 234 15 41.32383 41.49377 69621429008 7
9 234 23 40.26008 42.01927 15983792199 8
10 345 5 40.25700 41.69525 26203255328 5
# ... with 11 more rows
PROBLEM: This only calculates the square distances and times between successive rows, namely: row 1 and row 2, row 2 and row 3, row 3 and row 4
Is there a practical way that I can do this for all possible combinations of rows in each group?
I would like my output to have 6 calculations for each bid like this:
# A tibble: 21 x 6
bid ts latitude longitude dsq dt
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 123 23 38.41180 41.68493 178299634 23 (for rows 1 and 2)
2 123 45 42.20771 43.36318 198827672092 22 (for rows 1 and 3)
3 123 50 40.22803 43.00208 49480260636 5 (for rows 1 and 4)
4 123 12 38.46078 42.79847 5557152213 12 (for rows 2 and 3)
5 123 27 40.53698 42.57617 53781504422 15 (for rows 2 and 4)
6 123 19 39.04038 42.17070 28958550947 -8 (for rows 2 and 5)
I am new to R so any suggestions appreciated!