Hello Forum, I have a problem with the strptime function. With the ''data1'' dataset below it works fine, but with the ''data2'' dataset something goes wrong (see final line below). Both data1 and data2 are in exactly the same original format, the only difference is that they span different dates. Please help, since it is driving me nuts! Many thanks. Best wishes, Des -> data1=read.table("data1.txt",header=T,sep="\t")> datetime1=strptime(data1$Date, "%a %b %d %H:%M:%S %Y") #example line fromdata1 ''Tue Aug 16 03:00:01 2011''> summary(datetime1)Min. 1st Qu. Median Mean "2011-08-15 21:00:01 BST" "2011-10-08 01:00:01 BST" "2011-11-30 05:00:01 GMT" "2011-11-30 04:38:47 GMT" 3rd Qu. Max. "2012-01-22 09:00:01 GMT" "2012-03-15 13:00:01 GMT"> min(datetime1)[1] "2011-08-15 21:00:01 BST"> data2=read.table("data2.txt",header=T,sep="\t")> datetime2=strptime(data2$Date, "%a %b %d %H:%M:%S %Y") #example line fromdata2 ''Sun Nov 27 13:07:01 2011''> summary(datetime2)Min. 1st Qu. Median Mean "2011-11-27 01:07:01 GMT" "2012-01-09 20:07:01 GMT" "2012-02-22 15:07:01 GMT" "2012-02-22 15:26:16 GMT" 3rd Qu. Max. "2012-04-06 12:07:01 BST" "2012-05-20 07:07:01 BST"> min(datetime2)[1] NA [[alternative HTML version deleted]]
Dates and times are notoriously tricky to get right and the fact that the printed representation is so different from the internal (which is basically just a count of seconds) doesn't really help: to make this fully reproducible, could you supply us with the output of dput(head(read.table("data1.txt",header=T,sep="\t"), 30)) dput(head(read.table("data2.txt",header=T,sep="\t"), 30)) as well? Thanks! Michael On Sun, May 27, 2012 at 3:56 PM, Des Callaghan <des.callaghan at ecostudy.co.uk> wrote:> Hello Forum, > > > > I have a problem with the strptime function. With the 'data1' dataset below > it works fine, but with the 'data2' dataset something goes wrong (see final > line below). Both data1 and data2 are in exactly the same original format, > the only difference is that they span different dates. Please help, since it > is driving me nuts! Many thanks. > > > > Best wishes, > > Des > > - > >> data1=read.table("data1.txt",header=T,sep="\t") > >> datetime1=strptime(data1$Date, "%a %b %d %H:%M:%S %Y") #example line from > data1 'Tue Aug 16 03:00:01 2011' > >> summary(datetime1) > > ? ? ? ? ? ? ? ? ? ? Min. ? ? ? ? ? ? ? ? ? 1st Qu. > Median ? ? ? ? ? ? ? ? ? ? ?Mean > > "2011-08-15 21:00:01 BST" "2011-10-08 01:00:01 BST" "2011-11-30 05:00:01 > GMT" "2011-11-30 04:38:47 GMT" > > ? ? ? ? ? ? ? ? ?3rd Qu. ? ? ? ? ? ? ? ? ? ? ?Max. > > "2012-01-22 09:00:01 GMT" "2012-03-15 13:00:01 GMT" > >> min(datetime1) > > [1] "2011-08-15 21:00:01 BST" > >> data2=read.table("data2.txt",header=T,sep="\t") > >> datetime2=strptime(data2$Date, "%a %b %d %H:%M:%S %Y") #example line from > data2 'Sun Nov 27 13:07:01 2011' > >> summary(datetime2) > > ? ? ? ? ? ? ? ? ? ? Min. ? ? ? ? ? ? ? ? ? 1st Qu. > Median ? ? ? ? ? ? ? ? ? ? ?Mean > > "2011-11-27 01:07:01 GMT" "2012-01-09 20:07:01 GMT" "2012-02-22 15:07:01 > GMT" "2012-02-22 15:26:16 GMT" > > ? ? ? ? ? ? ? ? ?3rd Qu. ? ? ? ? ? ? ? ? ? ? ?Max. > > "2012-04-06 12:07:01 BST" "2012-05-20 07:07:01 BST" > >> min(datetime2) > > [1] NA > > > > > > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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.
Some of your dates are displayed BST and some GMT. High probablilty your dates span the break between summer time and regular time when certain hours do not exist. (in the US we go from 0200 directly to 0301 in the spring when we move from standard time to daylight time). 0230 would therefore be displayed as NA. You will need to take control of the time zone, probably by forcing GMT at all times. On Sun, May 27, 2012 at 3:56 PM, Des Callaghan <des.callaghan@ecostudy.co.uk> wrote:> Hello Forum, > > > > I have a problem with the strptime function. With the 'data1' dataset below > it works fine, but with the 'data2' dataset something goes wrong (see final > line below). Both data1 and data2 are in exactly the same original format, > the only difference is that they span different dates. Please help, since > it > is driving me nuts! Many thanks. > > > > Best wishes, > > Des > > - > > > data1=read.table("data1.txt",header=T,sep="\t") > > > datetime1=strptime(data1$Date, "%a %b %d %H:%M:%S %Y") #example line from > data1 'Tue Aug 16 03:00:01 2011' > > > summary(datetime1) > > Min. 1st Qu. > Median Mean > > "2011-08-15 21:00:01 BST" "2011-10-08 01:00:01 BST" "2011-11-30 05:00:01 > GMT" "2011-11-30 04:38:47 GMT" > > 3rd Qu. Max. > > "2012-01-22 09:00:01 GMT" "2012-03-15 13:00:01 GMT" > > > min(datetime1) > > [1] "2011-08-15 21:00:01 BST" > > > data2=read.table("data2.txt",header=T,sep="\t") > > > datetime2=strptime(data2$Date, "%a %b %d %H:%M:%S %Y") #example line from > data2 'Sun Nov 27 13:07:01 2011' > > > summary(datetime2) > > Min. 1st Qu. > Median Mean > > "2011-11-27 01:07:01 GMT" "2012-01-09 20:07:01 GMT" "2012-02-22 15:07:01 > GMT" "2012-02-22 15:26:16 GMT" > > 3rd Qu. Max. > > "2012-04-06 12:07:01 BST" "2012-05-20 07:07:01 BST" > > > min(datetime2) > > [1] NA > > > > > > > > > > > [[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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Fantastic, thanks very much Richard. The addition of ''tz="GMT"'' worked a treat. Best wishes, Des From: Richard M. Heiberger [mailto:rmh@temple.edu] Sent: 28 May 2012 02:09 To: Des Callaghan Cc: r-help@r-project.org Subject: Re: [R] Problem with strptime Some of your dates are displayed BST and some GMT. High probablilty your dates span the break between summer time and regular time when certain hours do not exist. (in the US we go from 0200 directly to 0301 in the spring when we move from standard time to daylight time). 0230 would therefore be displayed as NA. You will need to take control of the time zone, probably by forcing GMT at all times. On Sun, May 27, 2012 at 3:56 PM, Des Callaghan <des.callaghan@ecostudy.co.uk> wrote: Hello Forum, I have a problem with the strptime function. With the ''data1'' dataset below it works fine, but with the ''data2'' dataset something goes wrong (see final line below). Both data1 and data2 are in exactly the same original format, the only difference is that they span different dates. Please help, since it is driving me nuts! Many thanks. Best wishes, Des -> data1=read.table("data1.txt",header=T,sep="\t")> datetime1=strptime(data1$Date, "%a %b %d %H:%M:%S %Y") #example line fromdata1 ''Tue Aug 16 03:00:01 2011''> summary(datetime1)Min. 1st Qu. Median Mean "2011-08-15 21:00:01 BST" "2011-10-08 01:00:01 BST" "2011-11-30 05:00:01 GMT" "2011-11-30 04:38:47 GMT" 3rd Qu. Max. "2012-01-22 09:00:01 GMT" "2012-03-15 13:00:01 GMT"> min(datetime1)[1] "2011-08-15 21:00:01 BST"> data2=read.table("data2.txt",header=T,sep="\t")> datetime2=strptime(data2$Date, "%a %b %d %H:%M:%S %Y") #example line fromdata2 ''Sun Nov 27 13:07:01 2011''> summary(datetime2)Min. 1st Qu. Median Mean "2011-11-27 01:07:01 GMT" "2012-01-09 20:07:01 GMT" "2012-02-22 15:07:01 GMT" "2012-02-22 15:26:16 GMT" 3rd Qu. Max. "2012-04-06 12:07:01 BST" "2012-05-20 07:07:01 BST"> min(datetime2)[1] NA [[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. _____ No virus found in this message. Checked by AVG - www.avg.com [[alternative HTML version deleted]]