Skip to content
Prev 343565 / 398506 Next

Bus stop sequence matching problem

Homework? The list has a no homework policy - but perhaps I'll be forgiven por
posting hints.
In general terms, this is how I appraoched the problem:
* Loop through the rows of stop_onoff - for (idx in ...someething...) {...
* For each row, find the first of "ref" in a suitably filtered subset of
stop_sequence, and keep track of these row numbers
* Update columns "on" and "off"
* Use cumsum to calculate the number of passengers on the bus

Note the loop. Someone cleverer than I might be able to vectorise that step,
but I couldn't see how.

By the way, if this is homework...

Are you sure you're desired_output is correct? I would expect someething like
  seq ref on off load
1  10   A  5   0    5
2  20   B  0   0    5
3  30   C  0   0    5
4  40   D  0   2    3
5  50   B 10   2   11
6  60   A  0   6    5

Are you aware that you're "ref" ccolumns are factors and not characters? If
you use "stringsAsFactors = FALSE" or
stop_onoff <-
data.frame(ref=factor(c('A','D','B','A'), levels =
levels(stop_sequence$ref)),on=c(5,0,10,0),off=c(0,2,2,6))
it will simplify your'e analysis (or at least reduce some typing).

Type the following in an R console
?data.frame
?factor
and have a read.

Now, if this ain't homework, or you just want someone to do it for you, e-mail
me offline and I'll send you my appraoch. If it is homework, let me know - I'm
happy to help anyway, but I will be trying to help you solve this for
yourself.

Cheers,
DMcP

On Sat, 30 Aug 2014 12:46:17 +1200 Adam Lawrence <alaw005 at gmail.com> wrote
bus
certain
the stop
____________________________________________________________
South Africas premier free email service - www.webmail.co.za 

Cheapest Insurance Quotes!
https://www.outsurance.co.za/insurance-quote/personal/?source=msn&cr=Postit14_468x60_gif&cid=322