[R] Odp: Getting the month out of my date as a number not characters

Petr PIKAL petr.pikal at precheza.cz
Thu Jul 2 12:55:58 CEST 2009


Hi

r-help-bounces at r-project.org napsal dne 02.07.2009 12:40:05:

> I have a data frame (hf) that is all set up and the dates are working 
> fine - however I need to extract the months and hours (2 separate 
> columns) as numbers - however they are coming out as characters.
> 
> I have tried both the following:
> 
> hf50$hour=  hf50$date
> hf50$hour=format(hf50["hour"],"%H")
> 
> and
> 
> hf$month <- as.POSIXct(strptime(hf$date, format = "%m"))

If hf$date is in POSIX format then

format(hf$date,"%m")

shall give you month as a number 01-12. Actually it is a character and you 
need to convert it to numbers by as.numeric.

If it is in different format then depending on how it is actually 
formatted you can use strptime to transform it to POSIX class and then to 
use format to extract only days or months from it.

Regards
Petr



> 
> but they are still coming out as characters.
> 
> Any ideas please?
> Thanks,
> Tim.
> 
> ______________________________________________
> 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