Hi, I have following kind of dataset (all are dates) in my Excel sheet. 09/08/08 09/05/08 09/04/08 09/02/08 09/01/08 29/08/2008 28/08/2008 27/08/2008 26/08/2008 25/08/2008 22/08/2008 21/08/2008 20/08/2008 18/08/2008 14/08/2008 13/08/2008 08/12/08 08/11/08 08/08/08 08/07/08 However I want to use R to compile those data to make all dates in same format. Can anyone please tell me any automated way for doing that?
On Mon, 8 Sep 2008, Megh Dal wrote:> Hi, > > I have following kind of dataset (all are dates) in my Excel sheet. > > 09/08/08 > 09/05/08 > 09/04/08 > 09/02/08 > 09/01/08 > 29/08/2008 > 28/08/2008 > 27/08/2008 > 26/08/2008 > 25/08/2008 > 22/08/2008 > 21/08/2008 > 20/08/2008 > 18/08/2008 > 14/08/2008 > 13/08/2008 > 08/12/08 > 08/11/08 > 08/08/08 > 08/07/08 > > However I want to use R to compile those data to make all dates in same > format. Can anyone please tell me any automated way for doing that? >Well you have to read them in as character first. Then use sub to make the two digit years into four digits. The following could probably be improved by a regular expression whiz, but works:> strngs <- c("06/05/08","23/11/2008") > sub("([0-9][0-9]/[0-9][0-9]/)([0-9][0-9]$)","\\120\\2",strngs)[1] "06/05/2008" "23/11/2008" David Scott _________________________________________________________________ David Scott Department of Statistics, Tamaki Campus The University of Auckland, PB 92019 Auckland 1142, NEW ZEALAND Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000 Email: d.scott at auckland.ac.nz Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics
Try this: strptime(x, ifelse(nchar(x) == 8, '%d/%m/%y', '%d/%m/%Y')) On Tue, Sep 9, 2008 at 3:48 AM, Megh Dal <megh700004@yahoo.com> wrote:> Hi, > > I have following kind of dataset (all are dates) in my Excel sheet. > > 09/08/08 > 09/05/08 > 09/04/08 > 09/02/08 > 09/01/08 > 29/08/2008 > 28/08/2008 > 27/08/2008 > 26/08/2008 > 25/08/2008 > 22/08/2008 > 21/08/2008 > 20/08/2008 > 18/08/2008 > 14/08/2008 > 13/08/2008 > 08/12/08 > 08/11/08 > 08/08/08 > 08/07/08 > > However I want to use R to compile those data to make all dates in same > format. Can anyone please tell me any automated way for doing that? > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Is this Month-Day or Day-Month or a mixture of both? I still think using the Format -> Cell -> Date will work much better... el On 09 Sep 2008, at 11:21 , David Scott wrote:> On Mon, 8 Sep 2008, Megh Dal wrote: > >> Hi, >> >> I have following kind of dataset (all are dates) in my Excel sheet. >> >> 09/08/08 >> 09/05/08 >> 09/04/08 >> 09/02/08 >> 09/01/08 >> 29/08/2008 >> 28/08/2008 >> 27/08/2008 >> 26/08/2008 >> 25/08/2008 >> 22/08/2008 >> 21/08/2008 >> 20/08/2008 >> 18/08/2008 >> 14/08/2008 >> 13/08/2008 >> 08/12/08 >> 08/11/08 >> 08/08/08 >> 08/07/08 >> >> However I want to use R to compile those data to make all dates in >> same format. Can anyone please tell me any automated way for doing >> that? >> > > Well you have to read them in as character first. Then use sub to > make the two digit years into four digits. The following could > probably be improved by a regular expression whiz, but works: > >> strngs <- c("06/05/08","23/11/2008") >> sub("([0-9][0-9]/[0-9][0-9]/)([0-9][0-9]$)","\\120\\2",strngs) > [1] "06/05/2008" "23/11/2008" > > > David Scott
It is a mixture of both. The data is so notorious excel cant format properly. Therefore I thought whether R can do something otherwise I have to do manually. --- On Tue, 9/9/08, Dr Eberhard W Lisse <el at lisse.na> wrote:> From: Dr Eberhard W Lisse <el at lisse.na> > Subject: Re: [R] Compiling date > To: "David Scott" <d.scott at auckland.ac.nz> > Cc: "Dr Eberhard W Lisse" <el at lisse.na>, "Megh Dal" <megh700004 at yahoo.com>, r-help at stat.math.ethz.ch > Date: Tuesday, September 9, 2008, 11:37 PM > Is this Month-Day or Day-Month or a mixture of both? > > I still think using the Format -> Cell -> Date will > work > much better... > > el > > > On 09 Sep 2008, at 11:21 , David Scott wrote: > > > On Mon, 8 Sep 2008, Megh Dal wrote: > > > >> Hi, > >> > >> I have following kind of dataset (all are dates) > in my Excel sheet. > >> > >> 09/08/08 > >> 09/05/08 > >> 09/04/08 > >> 09/02/08 > >> 09/01/08 > >> 29/08/2008 > >> 28/08/2008 > >> 27/08/2008 > >> 26/08/2008 > >> 25/08/2008 > >> 22/08/2008 > >> 21/08/2008 > >> 20/08/2008 > >> 18/08/2008 > >> 14/08/2008 > >> 13/08/2008 > >> 08/12/08 > >> 08/11/08 > >> 08/08/08 > >> 08/07/08 > >> > >> However I want to use R to compile those data to > make all dates in > >> same format. Can anyone please tell me any > automated way for doing > >> that? > >> > > > > Well you have to read them in as character first. Then > use sub to > > make the two digit years into four digits. The > following could > > probably be improved by a regular expression whiz, but > works: > > > >> strngs <- > c("06/05/08","23/11/2008") > >> > sub("([0-9][0-9]/[0-9][0-9]/)([0-9][0-9]$)","\\120\\2",strngs) > > [1] "06/05/2008" "23/11/2008" > > > > > > David Scott