[R] Dataframe calculations

Erich Neuwirth erich.neuwirth at univie.ac.at
Fri Mar 19 16:38:02 CET 2010


with the following code

newvars()$ARRIVALS and newvars()$DEPARTURES
will give you the new variables you need.


-=-=-=


addDelays <- function(arriveTime,waitVec,travelVec){
  start<-as.POSIXct(arriveTime,format="%H:%M:%S")
  delays<-as.vector(t(cbind(waitVec,travelVec)))
  newtimes<-format(start+cumsum(delays)*60,format="%H:%M:%S")
  list(departs=c(arriveTime,(odds(newtimes))[-1]),
       arrives=evens(newtimes))
}

odds <- function(inVec){
  indvec<-0:(floor((length(inVec)-1)/2))
  inVec[2*indvec+1]
}

evens <- function(inVec){
  odds(inVec[-1])
}


newvars <- function(){
  DATE<-with(SCHEDULE2,paste(YEAR,MM,DD,sep=""))
  starts<-as.list(with(SCHEDULE2,tapply(ARRIVE,DATE,function(x)x[1])))
  waits<-with(SCHEDULE2,tapply(WAIT,DATE,function(x)x))
  travels<-with(SCHEDULE2,tapply(TRAVEL,DATE,function(x)x))
  list(DEPARTURES=

as.vector(mapply(function(...)addDelays(...)$departs,starts,waits,travels)),
    ARRIVALS=

as.vector(mapply(function(...)addDelays(...)$arrives,starts,waits,travels)))
}



-- 
Erich Neuwirth, University of Vienna
Faculty of Computer Science
Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39464 Fax: +43-1-4277-39459



More information about the R-help mailing list