Tim Chatterton
2009-Jul-02 10:40 UTC
[R] Getting the month out of my date as a number not characters
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")) but they are still coming out as characters. Any ideas please? Thanks, Tim.
Tim Chatterton
2009-Jul-02 10:40 UTC
[R] Getting the month out of my date as a number not characters
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")) but they are still coming out as characters. Any ideas please? Thanks, Tim.
Petr PIKAL
2009-Jul-02 10:55 UTC
[R] Odp: Getting the month out of my date as a number not characters
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Petr PIKAL
2009-Jul-02 11:28 UTC
[R] Odp: Getting the month out of my date as a number not characters
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 byas.numeric> > This is the bit I have problems with - I can easily get it out ascharacters> but I cannot work out how to use as.numeric (and had actually given upas 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 andyou> 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 thento> 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
Don MacQueen
2009-Jul-02 15:14 UTC
[R] Getting the month out of my date as a number not characters
Try hf$hour <- as.POSIXlt(hf$date)$hour hf$month <- as.POSIXlt(hf$date)$mon+1 To see why, use the man pages to study the structure of POSIXlt objects. Try:> tmp <- strptime("20/2/06 11:16:16.683", "%d/%m/%y %H:%M:%OS") > unclass(tmp)For example:> tmp <- strptime("20/2/06 11:16:16.683", "%d/%m/%y %H:%M:%OS") > foo <- data.frame(date=c(tmp,tmp+3600), x=1:2) > foodate x 1 2006-02-20 11:16:16 1 2 2006-02-20 12:16:16 2> foo$hour <- as.POSIXlt(foo$date)$hour > foo$mon <- as.POSIXlt(foo$date)$mon+1 > > foodate x hour mon 1 2006-02-20 11:16:16 1 11 2 2 2006-02-20 12:16:16 2 12 2 Just to be thorough, note:> class(tmp)[1] "POSIXt" "POSIXlt"> class(foo$date)[1] "POSIXt" "POSIXct" So strptime() creates objects of class POSIXlt, which are silently converted to POSIXct when put in a dataframe. Compare unclass(tmp) unclass(foo$date) -Don At 11:40 AM +0100 7/2/09, Tim Chatterton wrote:>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")) > >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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062