[R] Date conversion
Hi List, If I have a date format as: d <- "2001/1/1" I can easily convert it to number by using as.Date(d). But if I have d<-"1/1/2001", it does not work. Does anyone know how I can convert it using pre-written function in R? Regards, Alireza [[alternative HTML version deleted]] __ R-help@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.
[R] Date conversion
Hi R users, I have a factor variable called date as shown below: Can anyone share the best / most efficient way to extract year and week (e.g. year = 2006, week = 52 for first record, etc..)? My data set has 1 million records. DATE 11DEC2006 11SEP2006 01APR2007 02DEC2007 Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Date-conversion-tp22355788p22355788.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Date conversion
Dr. Alireza Zolfaghari wrote: > Hi List, > If I have a date format as: > d <- "2001/1/1" > I can easily convert it to number by using as.Date(d). Yes. If it means "January the 1st" and not "1st of January", that is... > But if I have d<-"1/1/2001", it does not work. Does anyone know how I can > convert it using pre-written function in R? as.Date has a format= argument. See the Examples section in help(as.Date). > Regards, > Alireza -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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.
Re: [R] Date conversion
Pele wrote: Hi R users, I have a factor variable called date as shown below: Can anyone share the best / most efficient way to extract year and week (e.g. year = 2006, week = 52 for first record, etc..)? My data set has 1 million records. DATE 11DEC2006 11SEP2006 01APR2007 02DEC2007 Since I am not in the correct locale: Sys.setlocale(locale="C") date <- strptime(DATE, "%d%B%Y") format(date, "%Y") format(date, "%W") # which is certainly not 52 Uwe Ligges Thanks in advance for any help! __ R-help@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.
Re: [R] Date conversion
Hi, There are possibly several ways to do this. My approach would be: dates <- strptime(as.character(DATE), "%d%b%Y") year <- dates$year + 1900 week <- floor(dates$yday/365 * 52) HTH, --sundar On Thu, Mar 5, 2009 at 8:58 AM, Pele wrote: > > Hi R users, > > I have a factor variable called date as shown below: Can anyone share the > best / most efficient way to extract year and week (e.g. year = 2006, week > = 52 for first record, etc..)? My data set has 1 million records. > > DATE > 11DEC2006 > 11SEP2006 > 01APR2007 > 02DEC2007 > > > Thanks in advance for any help! > -- > View this message in context: > http://www.nabble.com/Date-conversion-tp22355788p22355788.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > __ R-help@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.
Re: [R] Date conversion
Hi Uwe, You are correct - that was a type O (52) and thanks for you your suggestion that works.. Pele wrote: > > > > Hi R users, > > I have a factor variable called date as shown below: Can anyone share the > best / most efficient way to extract year and week (e.g. year = 2006, > week = 52 for first record, etc..)? My data set has 1 million records. > > DATE > 11DEC2006 > 11SEP2006 > 01APR2007 > 02DEC2007 > > > Thanks in advance for any help! > -- View this message in context: http://www.nabble.com/Date-conversion-tp22355788p22356526.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.