[R] Dataframe manipulation

Hosack, Michael mhosack at state.pa.us
Thu Mar 18 14:48:06 CET 2010


Hello R users,

I spent most of my workday yesterday trying unsuccessfully to 
write code that will perform a series of calculations on my 
dataframe (partial copy is attached). What I need to do is
multiply the 24 hr time in the 'DEPART' column by the time 
(in the 'TRAVEL' column (travel time in minutes), and store this 
result in the 'ARRIVE' column for the following 'SITE1' number. 
Then I need to add the 'WAIT' time (minutes) for that site's row 
to its 'ARRIVE' time to calculate the next 'DEPART' time and 
then proceed down the dataframe independently for each unique 'MM' 
by 'DD' combination. What I want to do I hope will be obvious after you 
view the dataframe. Basically, for each unique 'MM' (month) by 'DD'(day) grouping 
I have four sites that are surveyed sequentially, and the starting
site for a survey day was chosen randomly. The route is circular,
proceeding from site 101 to 104. I really hope someone can help 
me with this, because I am so close to finishing it.

BTW, the code that adds minutes to 24 hr time to create 24 hr time is as follows:

addTime2<-function(timeTxt,mins){
orig.date<-as.POSIXct(paste("2001-01-01",timeTxt))
new.Date<-orig.date+mins*60
new.Date<-strsplit(as.character(new.Date)," ")
new.Time<-(sapply(new.Date,"[",2))
return(new.Time)
}
SCHEDULE2$DEPART<-addTime2(SCHEDULE2$ARRIVE,SCHEDULE2$WAIT)
(code courtesy of Gustaf)

Thank you,

Mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Dataframe.pdf
Type: application/pdf
Size: 101190 bytes
Desc: Dataframe.pdf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100318/09e9e261/attachment.pdf>


More information about the R-help mailing list