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>
David, A quick correction. I have been posting for less than one month (my first posting was Mar 5, verify if so inclined). Thank you for making me aware of dput and dump capabilities, I will use them in future posts. Secondly, I would assume that all of us are either directly or indirectly seeking assistance with 'work' related R issues, either for a government agency, in school as preparation for future paid work, or in academics where one receives a salary for doing research, among other things. So, I don't see the relevance of your second point. I doubt anyone here is learning R for the fun of it. I would rather be fishing, lol. I think my attached dataframe clearly shows what I want to do, and all I really want is some specific guidance (i.e. given relevant functions, etc.). I would LOVE to code this on my own. Thank you for your help thus far. Mike -----Original Message----- From: David Winsemius [mailto:dwinsemius at comcast.net] Sent: Thursday, March 18, 2010 10:31 AM To: Hosack, Michael Subject: offlist Re: [R] Dataframe manipulation Here's why I am not taking this one one. You've been posting similar problems for the last 6 months and you still have not learned how to use dput or dump to create cut and pastable R object descriptions... plus this looks like its a job and you are asking us to do your work... but that's only of secondary importance. It's the first concern that is primary. You are asking us to figure out what your data really looks like and repeat lots of steps that are already done. -- David. On Mar 18, 2010, at 9:48 AM, Hosack, Michael wrote:> 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 > <Dataframe.pdf>______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Hi maybe you shall be a little bit more clear what you really want. It is hard to copy your data o R without some extensive fiddling, so we need to rely on complicated text description. It seems that ?ave could help you but it is hard to check. Basically I would recode ARRIVE and DEPART as POSIX class as I suggested before. Then you can do some arithmetic with it. At least you shall tell us what you want to be in second value for ARRIVE. If your task is not repeating and size of your data is not too big probably some looping could be also suitable. Or also zoo and function na.locf can help but without data, shrug. Sorry that I can not provide canned help but I am really not sure what the output shall be. Regards Petr r-help-bounces at r-project.org napsal dne 18.03.2010 14:48:06:> 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 asfollows:> > 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 > [p??loha Dataframe.pdf odstran?na u?ivatelem Petr PIKAL/CTCAP] > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.