Not sure if there is an R way to do this or a regular express way, but here is what I am trying to do. I've got lots of data where the format is HH:MM:SS, but I need to format it like HH:MM:00, i.e. round the second down to zero. What is the best way to do this? Thanks again. Jason
Have you looked at the chron package? It has a trunc.times function: ??"times" # would have shown this to you And the help page appears to provide exactly what was requested. ---------puzzlement follows-------- I did get somewhat unexpected results when I applied what seems to be the obvious modification to the first example on the chron function help page: > x <- chron(dates = dts, times = tms, format= c(dates = "m/d/y", times = "h:m") ) > x [1] (02/27/92 2303) (02/27/92 2229) (01/14/92 0103) (02/28/92 1821) (02/01/92 1656) Notice that the semicolon is missing. It's not missing when I use a full "h:m:s" > x <- chron(dates = dts, times = tms, format= c(dates = "m/d/y", times = "h:m:s") ) > x [1] (02/27/92 23:03:20) (02/27/92 22:29:56) (01/14/92 01:03:30) (02/28/92 18:21:03) [5] (02/01/92 16:56:26) But it comes back with "h:m:" > x <- chron(dates = dts, times = tms, format= c(dates = "m/d/y", times = "h:m:") ) > x [1] (02/27/92 23:03:) (02/27/92 22:29:) (01/14/92 01:03:) (02/28/92 18:21:) (02/01/92 16:56:) -- David On Jul 16, 2009, at 3:25 PM, Jason Rupert wrote:> > Not sure if there is an R way to do this or a regular express way, > but here is what I am trying to do. > > I've got lots of data where the format is HH:MM:SS, but I need to > format it like HH:MM:00, i.e. round the second down to zero. > > What is the best way to do this? > > Thanks again. > > Jason >David Winsemius, MD Heritage Laboratories West Hartford, CT
Hi,> Not sure if there is an R way to do this or a regular express way, > but here is what I am trying to do. > > I've got lots of data where the format is HH:MM:SS, but I need to > format it like HH:MM:00, i.e. round the second down to zero. > > What is the best way to do this?Probably not the best way, but here's one way to do it, step by step: R> a <- rep("HH:MM:SS", 5) R> a [1] "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" R> b <- strsplit(a, ":") R> b [[1]] [1] "HH" "MM" "SS" [[2]] [1] "HH" "MM" "SS" [[3]] [1] "HH" "MM" "SS" [[4]] [1] "HH" "MM" "SS" [[5]] [1] "HH" "MM" "SS" R> b2 <- lapply(b, function(pieces) c(pieces[1:2],"00")) R> b2 [[1]] [1] "HH" "MM" "00" [[2]] [1] "HH" "MM" "00" [[3]] [1] "HH" "MM" "00" [[4]] [1] "HH" "MM" "00" [[5]] [1] "HH" "MM" "00" R> a2 <- sapply(b2, paste, collapse=':') R> a2 [1] "HH:MM:00" "HH:MM:00" "HH:MM:00" "HH:MM:00" "HH:MM:00" HTH, -steve -- Steve Lianoglou Graduate Student: Physiology, Biophysics and Systems Biology Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
It your times are chron objects then trunc(tt, "minutes") where tt are your times may do what you want. It truncated but that seems to be what you want. --- On Thu, 7/16/09, Jason Rupert <jasonkrupert at yahoo.com> wrote:> From: Jason Rupert <jasonkrupert at yahoo.com> > Subject: [R] Best way to replace :SS with :00 > To: R-help at r-project.org > Received: Thursday, July 16, 2009, 3:25 PM > > Not sure if there is an R way to do this or a regular > express way, but here is what I am trying to do. > > I've got lots of data where the format is HH:MM:SS, but I > need to format it like HH:MM:00, i.e. round the second down > to zero.? > > What is the best way to do this? > > Thanks again. > > Jason > > ______________________________________________ > 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. >__________________________________________________________________ The new Internet Explorer? 8 - Faster, safer, easier. Optimized for Yahoo! Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/
probably : a <- rep("HH:MM:SS", 5) substring(a, 7) = "00" a Not sure if there is an R way to do this or a regular express way, but here is what I am trying to do. I've got lots of data where the format is HH:MM:SS, but I need to format it like HH:MM:00, i.e. round the second down to zero. What is the best way to do this? Thanks again. Jason [[alternative HTML version deleted]]
Dang it. I forgot to mention the actual format of the time is the following: "HH:MM:SS AM" or "HH:MM:SS PM" And I would still hope for them to end up with the following format: "HH:MM:00 AM" or "HH:MM:00 PM" How would you propose handling that condition? I tried to use strsplit with items to split on, but no luck. Thank you again for all your help. --- On Thu, 7/16/09, Steve Lianoglou <mailinglist.honeypot at gmail.com> wrote:> From: Steve Lianoglou <mailinglist.honeypot at gmail.com> > Subject: Re: [R] Best way to replace :SS with :00 > To: "Jason Rupert" <jasonkrupert at yahoo.com> > Cc: R-help at r-project.org > Date: Thursday, July 16, 2009, 2:58 PM > Hi, > > > Not sure if there is an R way to do this or a regular > express way, but here is what I am trying to do. > > > > I've got lots of data where the format is HH:MM:SS, > but I need to format it like HH:MM:00, i.e. round the second > down to zero. > > > > What is the best way to do this? > > > Probably not the best way, but here's one way to do it, > step by step: > > R> a <- rep("HH:MM:SS", 5) > R> a > [1] "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" > > R> b <- strsplit(a, ":") > R> b > [[1]] > [1] "HH" "MM" "SS" > > [[2]] > [1] "HH" "MM" "SS" > > [[3]] > [1] "HH" "MM" "SS" > > [[4]] > [1] "HH" "MM" "SS" > > [[5]] > [1] "HH" "MM" "SS" > > R> b2 <- lapply(b, function(pieces) > c(pieces[1:2],"00")) > R> b2 > [[1]] > [1] "HH" "MM" "00" > > [[2]] > [1] "HH" "MM" "00" > > [[3]] > [1] "HH" "MM" "00" > > [[4]] > [1] "HH" "MM" "00" > > [[5]] > [1] "HH" "MM" "00" > > R> a2 <- sapply(b2, paste, collapse=':') > R> a2 > [1] "HH:MM:00" "HH:MM:00" "HH:MM:00" "HH:MM:00" "HH:MM:00" > > HTH, > -steve > > -- > Steve Lianoglou > Graduate Student: Physiology, Biophysics and Systems > Biology > Weill Medical College of Cornell University > > Contact Info: http://cbio.mskcc.org/~lianos/contact > > > >
This is awesome! Total continue to be amazed. Thanks again! --- On Thu, 7/16/09, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> From: Gabor Grothendieck <ggrothendieck at gmail.com> > Subject: Re: [R] Best way to replace :SS with :00 > To: "Jason Rupert" <jasonkrupert at yahoo.com> > Cc: R-help at r-project.org > Date: Thursday, July 16, 2009, 3:45 PM > Try the following which replaces : > followed by any two > characters followed by space with the required string: > > > x <- c("HH:MM:SS AM", "HH:MM:SS PM") > > sub(":.. ", ":00 ", x) > [1] "HH:MM:00 AM" "HH:MM:00 PM" > > > On Thu, Jul 16, 2009 at 4:20 PM, Jason Rupert<jasonkrupert at yahoo.com> > wrote: > > > > Dang it. ?I forgot to mention the actual format of > the time is the following: > > "HH:MM:SS AM" or "HH:MM:SS PM" > > And I would still hope for them to end up with the > following format: > > "HH:MM:00 AM" or "HH:MM:00 PM" > > > > How would you propose handling that condition? > > > > I tried to use strsplit with items to split on, but no > luck. > > > > Thank you again for all your help. > > > > > > > > > > --- On Thu, 7/16/09, Steve Lianoglou <mailinglist.honeypot at gmail.com> > wrote: > > > >> From: Steve Lianoglou <mailinglist.honeypot at gmail.com> > >> Subject: Re: [R] Best way to replace :SS with :00 > >> To: "Jason Rupert" <jasonkrupert at yahoo.com> > >> Cc: R-help at r-project.org > >> Date: Thursday, July 16, 2009, 2:58 PM > >> Hi, > >> > >> > Not sure if there is an R way to do this or a > regular > >> express way, but here is what I am trying to do. > >> > > >> > I've got lots of data where the format is > HH:MM:SS, > >> but I need to format it like HH:MM:00, i.e. round > the second > >> down to zero. > >> > > >> > What is the best way to do this? > >> > >> > >> Probably not the best way, but here's one way to > do it, > >> step by step: > >> > >> R> a <- rep("HH:MM:SS", 5) > >> R> a > >> [1] "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" "HH:MM:SS" > "HH:MM:SS" > >> > >> R> b <- strsplit(a, ":") > >> R> b > >> [[1]] > >> [1] "HH" "MM" "SS" > >> > >> [[2]] > >> [1] "HH" "MM" "SS" > >> > >> [[3]] > >> [1] "HH" "MM" "SS" > >> > >> [[4]] > >> [1] "HH" "MM" "SS" > >> > >> [[5]] > >> [1] "HH" "MM" "SS" > >> > >> R> b2 <- lapply(b, function(pieces) > >> c(pieces[1:2],"00")) > >> R> b2 > >> [[1]] > >> [1] "HH" "MM" "00" > >> > >> [[2]] > >> [1] "HH" "MM" "00" > >> > >> [[3]] > >> [1] "HH" "MM" "00" > >> > >> [[4]] > >> [1] "HH" "MM" "00" > >> > >> [[5]] > >> [1] "HH" "MM" "00" > >> > >> R> a2 <- sapply(b2, paste, collapse=':') > >> R> a2 > >> [1] "HH:MM:00" "HH:MM:00" "HH:MM:00" "HH:MM:00" > "HH:MM:00" > >> > >> HTH, > >> -steve > >> > >> -- > >> Steve Lianoglou > >> Graduate Student: Physiology, Biophysics and > Systems > >> Biology > >> Weill Medical College of Cornell University > >> > >> Contact Info: http://cbio.mskcc.org/~lianos/contact > >> > >> > >> > >> > > > > ______________________________________________ > > 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. > > >