Hi everybody. I have a dataframe that contains a factor with the date information in the format like in the example below: "2009/05/12 11:22:31 AM" I have been able to convert it to POSIXt using strptime Now I want to print only the date as one vector and the time in another vector but they must be real date and time classes so that i can use them in calculations to reconstruct my situation: date=as.factor("2009/05/12 11:22:31 AM") posix.date=strptime(date, "%Y/%m/%d %I:%M:%S %p") I tried: time=format(posix.date, "%A %H:%M") # For weekday, hour and minute but the class in character. Now it prints like i want but it cannot be used in calculations. I would appreciate any suggestions Thanks in advance Christiaan [[alternative HTML version deleted]]
Try this and see R News 4/1 for more.> now <- Sys.time(); now[1] "2009-06-10 07:26:23 EDT"> library(chron) > ch <- as.chron(format(now)) > dd <- dates(ch); dd; dd+1day 06/10/09 day 06/11/09> tt <- times(ch) - times(dd); tt; tt+1/24day 07:26:23 day 08:26:23 On Wed, Jun 10, 2009 at 7:13 AM, christiaan pauw<cjpauw at gmail.com> wrote:> Hi everybody. I have a dataframe that contains a factor with the date > information in the format like in the example below: > "2009/05/12 11:22:31 AM" > > I have been able to convert it to POSIXt using strptime > > Now I want to print only the date as one vector and the time in another > vector but they must be real date and time classes so that i can use them in > calculations > > to reconstruct my situation: > > date=as.factor("2009/05/12 11:22:31 AM") > posix.date=strptime(date, "%Y/%m/%d %I:%M:%S %p") > > I tried: > > time=format(posix.date, "%A %H:%M") # For weekday, hour and minute > > but the class in character. Now it prints like i want but it cannot be used > in calculations. > > I would appreciate any suggestions > > Thanks in advance > Christiaan > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
There is no class for times only in R. Your best bet is probably a difftime, e.g. time <- difftime(posix.date, as.Date(posix.date), units="secs") Allan. christiaan pauw wrote:> Hi everybody. I have a dataframe that contains a factor with the date > information in the format like in the example below: > "2009/05/12 11:22:31 AM" > > I have been able to convert it to POSIXt using strptime > > Now I want to print only the date as one vector and the time in another > vector but they must be real date and time classes so that i can use them in > calculations > > to reconstruct my situation: > > date=as.factor("2009/05/12 11:22:31 AM") > posix.date=strptime(date, "%Y/%m/%d %I:%M:%S %p") > > I tried: > > time=format(posix.date, "%A %H:%M") # For weekday, hour and minute > > but the class in character. Now it prints like i want but it cannot be used > in calculations. > > I would appreciate any suggestions > > Thanks in advance > Christiaan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >