Skip to content
Prev 343567 / 398506 Next

Bus stop sequence matching problem

Try dtw.  First convert ref to numeric since dtw does not handle
character input.  Then align using dtw and NA out repeated values in
the alignment.  Finally zap ugly row names and calculate loading:

library(dtw)
s1 <- as.numeric(stop_sequence$ref)
s2 <- as.numeric(factor(as.character(stop_onoff$ref),
levels(stop_sequence$ref)))
a <- dtw(s1, s2)
DF <- cbind(stop_sequence,
      stop_onoff[replace(a$index2, c(FALSE, diff(a$index2) == 0), NA), ])[-3]
rownames(DF) <- NULL
transform(DF, loading = cumsum(ifelse(is.na(on), 0, on)) -
                        cumsum(ifelse(is.na(off), 0, off)))

giving:

  seq ref on off loading
1  10   A  5   0       5
2  20   B NA  NA       5
3  30   C NA  NA       5
4  40   D  0   2       3
5  50   B 10   2      11
6  60   A  0   6       5

You will need to test this with more data and tweak it if necessary
via the various dtw arguments.
On Fri, Aug 29, 2014 at 8:46 PM, Adam Lawrence <alaw005 at gmail.com> wrote: