[R] isolating Hour and minute form date and time

Allan Engelhardt allane at cybaea.com
Wed Jun 10 13:30:25 CEST 2009


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.
>




More information about the R-help mailing list