[R] Odp: Getting the month out of my date as a number not characters
Petr PIKAL
petr.pikal at precheza.cz
Thu Jul 2 13:28:21 CEST 2009
Hi
Tim Chatterton <tim.chatterton at uwe.ac.uk> napsal dne 02.07.2009 13:19:46:
> > Actually it is a character and you need to convert it to numbers by
as.numeric
>
> This is the bit I have problems with - I can easily get it out as
characters
> but I cannot work out how to use as.numeric (and had actually given up
as it
> seemed to only want to work on numeric fields not character fields).
>
> Any idea how I do the character to numeric conversion?
> Thanks,
> Tim.
What is the problem with
> format(strptime("20/2/06 11:16:16.683", "%d/%m/%y %H:%M:%OS"), "%m")
[1] "02"
> as.numeric(format(strptime("20/2/06 11:16:16.683", "%d/%m/%y
%H:%M:%OS"), "%m"))
[1] 2
Regards
Petr
>
>
> Petr PIKAL wrote:
> 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.
>
>
>
>
> This incoming email to UWE has been independently scanned for viruses by
> McAfee anti-virus software and none were detected
>
>
> --
>
>
> __________________________________________
>
> Dr Tim Chatterton
> Senior Research Fellow
> Air Quality Management Resource Centre
> Faculty of Environment and Technology
> University of the West of England
> Frenchay Campus
> Bristol
> BS16 1QY
>
> Tel: 0117 328 2929
> Fax: 0117 328 3360
> Email: Tim.Chatterton at uwe.ac.uk
More information about the R-help
mailing list