Incorrect Conversion of Datetime

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Incorrect Conversion of Datetime

Ogbos
Dear Friends,
A sample of my data is:
98 05 01 02    8541
98 05 01 03    8548
98 05 01 04    8512
98 05 01 05    8541
98 05 01 06    8509
98 05 01 07    8472
98 05 01 08    8454
98 05 01 09    8461
98 05 01 10    8462
98 05 01 11    8475
98 05 01 12    8433
98 05 01 13    8479
98 05 01 14    8417
98 05 01 15    8463
98 05 01 16    8473
98 05 01 17    8450
98 05 01 18    8433
98 05 01 19    8437
98 05 01 20    8437
98 05 01 21    8438
98 05 01 22    8421
98 05 01 23    8420
98 05 02 00    8371
98 05 02 01    8338
98 05 02 02    8251
98 05 02 03    8204
98 05 02 04    8183
98 05 02 05    8231
98 05 02 06    8242
Columns 1, 2, 3, 4 and 5 stands for year, month, day , hour and count.

Using:
Sys.setenv( TZ="GMT" )


dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
"hour", "counts"))
dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
a =  dta$datetime
I converted the datetime and plotted the graph of count vs a. The plot
was great but I have issues with the date.

The raw data is for some hours for Ist and second day of may 1998 as
is evident from the sample data. But the result of date stored in "a"
above shows:
> a
 [1] "1998-01-05 02:00:00 GMT" "1998-01-05 03:00:00 GMT"
 [3] "1998-01-05 04:00:00 GMT" "1998-01-05 05:00:00 GMT"
 [5] "1998-01-05 06:00:00 GMT" "1998-01-05 07:00:00 GMT"
 [7] "1998-01-05 08:00:00 GMT" "1998-01-05 09:00:00 GMT"
 [9] "1998-01-05 10:00:00 GMT" "1998-01-05 11:00:00 GMT"
[11] "1998-01-05 12:00:00 GMT" "1998-01-05 13:00:00 GMT"
[13] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
[15] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
[17] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
[19] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
[21] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
[23] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
[25] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
[27] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
[29] "1998-01-06 06:00:00 GMT"
This seems to suggest day 5 and 6 in January 1998 instead of day 1 and
2 in May of 1998.

I have spent some time trying to resolve this but I have not been successful.

I would be thankful if you could help me to check where I went astray.

Thank you.
Best wishes
Ogbos

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Jim Lemon-4
Hi Ogbos,
I get the correct result using strptime:

dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
 "%y %m %d %H"
)
> dta$date
[1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
[3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
[5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
[7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
[9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
[11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
[13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
[15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
[17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
[19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
[21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
[23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
[25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
[27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
[29] "1998-05-02 06:00:00 AEST"

Same result with

ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))

As it should be as the lower two call strptime. I can't see from your
code why the month and day are transcribed.

Jim

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Jim Lemon-4
Hi again,
Small typo, should be

dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
 "%Y %m %d %H"

as I tried it both with and without the century just to check and
copied the wrong line.

On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <[hidden email]> wrote:

>
> Hi Ogbos,
> I get the correct result using strptime:
>
> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>  "%y %m %d %H"
> )
> > dta$date
> [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
> [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
> [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
> [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
> [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
> [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
> [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
> [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
> [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
> [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
> [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
> [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
> [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
> [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
> [29] "1998-05-02 06:00:00 AEST"
>
> Same result with
>
> ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
> as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
>
> As it should be as the lower two call strptime. I can't see from your
> code why the month and day are transcribed.
>
> Jim

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Ogbos
Dear Jim,
Thank you for coming my assist me.
I have tried all you suggested but the same result keep coming.
I tried, for example:
dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
"hour", "counts"))

dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
 "%Y %m %d %H")
a =  dta$date
and obtained:
> a
 [1] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
 [3] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
 [5] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
 [7] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
 [9] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
[11] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
[13] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
[15] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
[17] "1998-01-06 06:00:00 GMT"

Instead of getting AEST as in your own result, mine remains GMT.

I think the problem is coming from my system or location, I am not sure.

Please have a look again and advise further.

Thank you.

On Wed, Jan 8, 2020 at 11:05 AM Jim Lemon <[hidden email]> wrote:

>
> Hi again,
> Small typo, should be
>
> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>  "%Y %m %d %H"
>
> as I tried it both with and without the century just to check and
> copied the wrong line.
>
> On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <[hidden email]> wrote:
> >
> > Hi Ogbos,
> > I get the correct result using strptime:
> >
> > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
> >  "%y %m %d %H"
> > )
> > > dta$date
> > [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
> > [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
> > [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
> > [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
> > [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
> > [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
> > [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
> > [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
> > [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
> > [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
> > [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
> > [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
> > [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
> > [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
> > [29] "1998-05-02 06:00:00 AEST"
> >
> > Same result with
> >
> > ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
> > as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
> >
> > As it should be as the lower two call strptime. I can't see from your
> > code why the month and day are transcribed.
> >
> > Jim

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Enrico Schumann-2
In reply to this post by Ogbos

Quoting Ogbos Okike <[hidden email]>:

> Dear Friends,
> A sample of my data is:
> 98 05 01 02    8541
> 98 05 01 03    8548
> 98 05 01 04    8512
> 98 05 01 05    8541
> 98 05 01 06    8509
> 98 05 01 07    8472
> 98 05 01 08    8454
> 98 05 01 09    8461
> 98 05 01 10    8462
> 98 05 01 11    8475
> 98 05 01 12    8433
> 98 05 01 13    8479
> 98 05 01 14    8417
> 98 05 01 15    8463
> 98 05 01 16    8473
> 98 05 01 17    8450
> 98 05 01 18    8433
> 98 05 01 19    8437
> 98 05 01 20    8437
> 98 05 01 21    8438
> 98 05 01 22    8421
> 98 05 01 23    8420
> 98 05 02 00    8371
> 98 05 02 01    8338
> 98 05 02 02    8251
> 98 05 02 03    8204
> 98 05 02 04    8183
> 98 05 02 05    8231
> 98 05 02 06    8242
> Columns 1, 2, 3, 4 and 5 stands for year, month, day , hour and count.
>
> Using:
> Sys.setenv( TZ="GMT" )
>
>
> dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
> "hour", "counts"))
> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
> a =  dta$datetime
> I converted the datetime and plotted the graph of count vs a. The plot
> was great but I have issues with the date.
>
> The raw data is for some hours for Ist and second day of may 1998 as
> is evident from the sample data. But the result of date stored in "a"
> above shows:
>> a
>  [1] "1998-01-05 02:00:00 GMT" "1998-01-05 03:00:00 GMT"
>  [3] "1998-01-05 04:00:00 GMT" "1998-01-05 05:00:00 GMT"
>  [5] "1998-01-05 06:00:00 GMT" "1998-01-05 07:00:00 GMT"
>  [7] "1998-01-05 08:00:00 GMT" "1998-01-05 09:00:00 GMT"
>  [9] "1998-01-05 10:00:00 GMT" "1998-01-05 11:00:00 GMT"
> [11] "1998-01-05 12:00:00 GMT" "1998-01-05 13:00:00 GMT"
> [13] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
> [15] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
> [17] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
> [19] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
> [21] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
> [23] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
> [25] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
> [27] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
> [29] "1998-01-06 06:00:00 GMT"
> This seems to suggest day 5 and 6 in January 1998 instead of day 1 and
> 2 in May of 1998.
>
> I have spent some time trying to resolve this but I have not been successful.
>
> I would be thankful if you could help me to check where I went astray.
>
> Thank you.
> Best wishes
> Ogbos
>

I cannot reproduce these results. Could you please provide a fully
reproducible example, by providing a small example dataset via 'dput(dta)'?


--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Ogbos
In reply to this post by Ogbos
Dear Jim,
In order to check whether I have a correct time on my system, I run:
$ timedatectl

 and it gives:
Local time: Wed 2020-01-08 13:03:44 WAT
                  Universal time: Wed 2020-01-08 12:03:44 UTC
                        RTC time: Wed 2020-01-08 12:03:44
                       Time zone: Africa/Lagos (WAT, +0100)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no.

The time is correct as it agrees with the system time.

I don't know if there are other things I need to change.

Thank you for any suggestions.
Ogbos

On Wed, Jan 8, 2020 at 12:55 PM Ogbos Okike <[hidden email]> wrote:

>
> Dear Jim,
> Thank you for coming my assist me.
> I have tried all you suggested but the same result keep coming.
> I tried, for example:
> dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
> "hour", "counts"))
>
> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>  "%Y %m %d %H")
> a =  dta$date
> and obtained:
> > a
>  [1] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
>  [3] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
>  [5] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
>  [7] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
>  [9] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
> [11] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
> [13] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
> [15] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
> [17] "1998-01-06 06:00:00 GMT"
>
> Instead of getting AEST as in your own result, mine remains GMT.
>
> I think the problem is coming from my system or location, I am not sure.
>
> Please have a look again and advise further.
>
> Thank you.
>
> On Wed, Jan 8, 2020 at 11:05 AM Jim Lemon <[hidden email]> wrote:
> >
> > Hi again,
> > Small typo, should be
> >
> > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
> >  "%Y %m %d %H"
> >
> > as I tried it both with and without the century just to check and
> > copied the wrong line.
> >
> > On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <[hidden email]> wrote:
> > >
> > > Hi Ogbos,
> > > I get the correct result using strptime:
> > >
> > > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
> > >  "%y %m %d %H"
> > > )
> > > > dta$date
> > > [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
> > > [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
> > > [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
> > > [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
> > > [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
> > > [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
> > > [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
> > > [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
> > > [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
> > > [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
> > > [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
> > > [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
> > > [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
> > > [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
> > > [29] "1998-05-02 06:00:00 AEST"
> > >
> > > Same result with
> > >
> > > ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
> > > as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
> > >
> > > As it should be as the lower two call strptime. I can't see from your
> > > code why the month and day are transcribed.
> > >
> > > Jim

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Jeff Newmiller
In your first email you said you were using

Sys.setenv( TZ="GMT" )

in your code, which defines the default assumption for time conversion timezone (at least until you change it). Keep in mind that you may be dealing with data from other timezones than your local one that the operating system uses, so the OS timezone only gets used if TZ is blank (and even that behavior can be OS-dependent I think).

Read

?OlsonNames

because AEST may not be a valid specification for TZ.

On January 8, 2020 4:09:01 AM PST, Ogbos Okike <[hidden email]> wrote:

>Dear Jim,
>In order to check whether I have a correct time on my system, I run:
>$ timedatectl
>
> and it gives:
>Local time: Wed 2020-01-08 13:03:44 WAT
>                  Universal time: Wed 2020-01-08 12:03:44 UTC
>                        RTC time: Wed 2020-01-08 12:03:44
>                       Time zone: Africa/Lagos (WAT, +0100)
>       System clock synchronized: yes
>systemd-timesyncd.service active: yes
>                 RTC in local TZ: no.
>
>The time is correct as it agrees with the system time.
>
>I don't know if there are other things I need to change.
>
>Thank you for any suggestions.
>Ogbos
>
>On Wed, Jan 8, 2020 at 12:55 PM Ogbos Okike <[hidden email]>
>wrote:
>>
>> Dear Jim,
>> Thank you for coming my assist me.
>> I have tried all you suggested but the same result keep coming.
>> I tried, for example:
>> dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
>> "hour", "counts"))
>>
>> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
>> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>>  "%Y %m %d %H")
>> a =  dta$date
>> and obtained:
>> > a
>>  [1] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
>>  [3] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
>>  [5] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
>>  [7] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
>>  [9] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
>> [11] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
>> [13] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
>> [15] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
>> [17] "1998-01-06 06:00:00 GMT"
>>
>> Instead of getting AEST as in your own result, mine remains GMT.
>>
>> I think the problem is coming from my system or location, I am not
>sure.
>>
>> Please have a look again and advise further.
>>
>> Thank you.
>>
>> On Wed, Jan 8, 2020 at 11:05 AM Jim Lemon <[hidden email]>
>wrote:
>> >
>> > Hi again,
>> > Small typo, should be
>> >
>> > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >  "%Y %m %d %H"
>> >
>> > as I tried it both with and without the century just to check and
>> > copied the wrong line.
>> >
>> > On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <[hidden email]>
>wrote:
>> > >
>> > > Hi Ogbos,
>> > > I get the correct result using strptime:
>> > >
>> > > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> > >  "%y %m %d %H"
>> > > )
>> > > > dta$date
>> > > [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
>> > > [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
>> > > [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
>> > > [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
>> > > [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
>> > > [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
>> > > [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
>> > > [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
>> > > [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
>> > > [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
>> > > [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
>> > > [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
>> > > [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
>> > > [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
>> > > [29] "1998-05-02 06:00:00 AEST"
>> > >
>> > > Same result with
>> > >
>> > > ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
>> > >
>as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
>> > >
>> > > As it should be as the lower two call strptime. I can't see from
>your
>> > > code why the month and day are transcribed.
>> > >
>> > > Jim
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Ogbos
In reply to this post by Enrico Schumann-2
Dear Enrico,
Thanks for your time.
I have tried to learn how to use dput in R. I have not yet made much progress.

I have succeeded in using dput to store my data frame. I first
converted my data into a data frame and then used:
dput(dd,file="Ogbos2",control = c("keepNA", "keepInteger",
"showAttributes")) to output the dput file. dd is my data frame.

When I opened the file, I didn't like its content as it differs very
much from my data frame. But I don't know whether that makes sense to
you. I am attaching the file.
I am thanking you in advance for additional suggestions.
Best wishes
Ogbos

On Wed, Jan 8, 2020 at 1:07 PM Enrico Schumann <[hidden email]> wrote:

>
>
> Quoting Ogbos Okike <[hidden email]>:
>
> > Dear Friends,
> > A sample of my data is:
> > 98 05 01 02    8541
> > 98 05 01 03    8548
> > 98 05 01 04    8512
> > 98 05 01 05    8541
> > 98 05 01 06    8509
> > 98 05 01 07    8472
> > 98 05 01 08    8454
> > 98 05 01 09    8461
> > 98 05 01 10    8462
> > 98 05 01 11    8475
> > 98 05 01 12    8433
> > 98 05 01 13    8479
> > 98 05 01 14    8417
> > 98 05 01 15    8463
> > 98 05 01 16    8473
> > 98 05 01 17    8450
> > 98 05 01 18    8433
> > 98 05 01 19    8437
> > 98 05 01 20    8437
> > 98 05 01 21    8438
> > 98 05 01 22    8421
> > 98 05 01 23    8420
> > 98 05 02 00    8371
> > 98 05 02 01    8338
> > 98 05 02 02    8251
> > 98 05 02 03    8204
> > 98 05 02 04    8183
> > 98 05 02 05    8231
> > 98 05 02 06    8242
> > Columns 1, 2, 3, 4 and 5 stands for year, month, day , hour and count.
> >
> > Using:
> > Sys.setenv( TZ="GMT" )
> >
> >
> > dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
> > "hour", "counts"))
> > dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> > dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
> > a =  dta$datetime
> > I converted the datetime and plotted the graph of count vs a. The plot
> > was great but I have issues with the date.
> >
> > The raw data is for some hours for Ist and second day of may 1998 as
> > is evident from the sample data. But the result of date stored in "a"
> > above shows:
> >> a
> >  [1] "1998-01-05 02:00:00 GMT" "1998-01-05 03:00:00 GMT"
> >  [3] "1998-01-05 04:00:00 GMT" "1998-01-05 05:00:00 GMT"
> >  [5] "1998-01-05 06:00:00 GMT" "1998-01-05 07:00:00 GMT"
> >  [7] "1998-01-05 08:00:00 GMT" "1998-01-05 09:00:00 GMT"
> >  [9] "1998-01-05 10:00:00 GMT" "1998-01-05 11:00:00 GMT"
> > [11] "1998-01-05 12:00:00 GMT" "1998-01-05 13:00:00 GMT"
> > [13] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
> > [15] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
> > [17] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
> > [19] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
> > [21] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
> > [23] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
> > [25] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
> > [27] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
> > [29] "1998-01-06 06:00:00 GMT"
> > This seems to suggest day 5 and 6 in January 1998 instead of day 1 and
> > 2 in May of 1998.
> >
> > I have spent some time trying to resolve this but I have not been successful.
> >
> > I would be thankful if you could help me to check where I went astray.
> >
> > Thank you.
> > Best wishes
> > Ogbos
> >
>
> I cannot reproduce these results. Could you please provide a fully
> reproducible example, by providing a small example dataset via 'dput(dta)'?
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Ogbos
In reply to this post by Jeff Newmiller
Dear Jeff,
Thank you very much for looking into this.

I have made some search on ?OlsonNames.
System time zones indicates Africa/Lagos whereas the data I am trying
to convert are Cosmic ray data prepared at different time zones
including Oulu station (Russia), Climax station (America), many parts
of Europe, etc.

Including:
 Sys.timezone()
str(OlsonNames())

in my code gives:
[1] "1998-01-05 02:00:00 WAT" "1998-01-05 03:00:00 WAT"
 [3] "1998-01-05 04:00:00 WAT" "1998-01-05 05:00:00 WAT"
 [5] "1998-01-05 06:00:00 WAT" "1998-01-05 07:00:00 WAT"
 [7] "1998-01-05 08:00:00 WAT" "1998-01-05 09:00:00 WAT"
 [9] "1998-01-05 10:00:00 WAT" "1998-01-05 11:00:00 WAT"
[11] "1998-01-05 12:00:00 WAT" "1998-01-05 13:00:00 WAT"
[13] "1998-01-05 14:00:00 WAT" "1998-01-05 15:00:00 WAT"
[15] "1998-01-05 16:00:00 WAT" "1998-01-05 17:00:00 WAT"
[17] "1998-01-05 18:00:00 WAT" "1998-01-05 19:00:00 WAT"
[19] "1998-01-05 20:00:00 WAT" "1998-01-05 21:00:00 WAT"
[21] "1998-01-05 22:00:00 WAT" "1998-01-05 23:00:00 WAT"
[23] "1998-01-06 00:00:00 WAT" "1998-01-06 01:00:00 WAT"
[25] "1998-01-06 02:00:00 WAT" "1998-01-06 03:00:00 WAT"
[27] "1998-01-06 04:00:00 WAT" "1998-01-06 05:00:00 WAT"
[29] "1998-01-06 06:00:00 WAT"

Replacing the two lines above with:
Sys.setenv( TZ="" )
gives:
[1] "1998-01-05 02:00:00 UTC" "1998-01-05 03:00:00 UTC"
 [3] "1998-01-05 04:00:00 UTC" "1998-01-05 05:00:00 UTC"
 [5] "1998-01-05 06:00:00 UTC" "1998-01-05 07:00:00 UTC"
 [7] "1998-01-05 08:00:00 UTC" "1998-01-05 09:00:00 UTC"
 [9] "1998-01-05 10:00:00 UTC" "1998-01-05 11:00:00 UTC"
[11] "1998-01-05 12:00:00 UTC" "1998-01-05 13:00:00 UTC"
[13] "1998-01-05 14:00:00 UTC" "1998-01-05 15:00:00 UTC"
[15] "1998-01-05 16:00:00 UTC" "1998-01-05 17:00:00 UTC"
[17] "1998-01-05 18:00:00 UTC" "1998-01-05 19:00:00 UTC"
[19] "1998-01-05 20:00:00 UTC" "1998-01-05 21:00:00 UTC"
[21] "1998-01-05 22:00:00 UTC" "1998-01-05 23:00:00 UTC"
[23] "1998-01-06 00:00:00 UTC" "1998-01-06 01:00:00 UTC"
[25] "1998-01-06 02:00:00 UTC" "1998-01-06 03:00:00 UTC"
[27] "1998-01-06 04:00:00 UTC" "1998-01-06 05:00:00 UTC"
[29] "1998-01-06 06:00:00 UTC"

All these are still not correct and I am yet longing for further help.

Best regards
Ogbos

On Wed, Jan 8, 2020 at 4:22 PM Jeff Newmiller <[hidden email]> wrote:

>
> In your first email you said you were using
>
> Sys.setenv( TZ="GMT" )
>
> in your code, which defines the default assumption for time conversion timezone (at least until you change it). Keep in mind that you may be dealing with data from other timezones than your local one that the operating system uses, so the OS timezone only gets used if TZ is blank (and even that behavior can be OS-dependent I think).
>
> Read
>
> ?OlsonNames
>
> because AEST may not be a valid specification for TZ.
>
> On January 8, 2020 4:09:01 AM PST, Ogbos Okike <[hidden email]> wrote:
> >Dear Jim,
> >In order to check whether I have a correct time on my system, I run:
> >$ timedatectl
> >
> > and it gives:
> >Local time: Wed 2020-01-08 13:03:44 WAT
> >                  Universal time: Wed 2020-01-08 12:03:44 UTC
> >                        RTC time: Wed 2020-01-08 12:03:44
> >                       Time zone: Africa/Lagos (WAT, +0100)
> >       System clock synchronized: yes
> >systemd-timesyncd.service active: yes
> >                 RTC in local TZ: no.
> >
> >The time is correct as it agrees with the system time.
> >
> >I don't know if there are other things I need to change.
> >
> >Thank you for any suggestions.
> >Ogbos
> >
> >On Wed, Jan 8, 2020 at 12:55 PM Ogbos Okike <[hidden email]>
> >wrote:
> >>
> >> Dear Jim,
> >> Thank you for coming my assist me.
> >> I have tried all you suggested but the same result keep coming.
> >> I tried, for example:
> >> dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
> >> "hour", "counts"))
> >>
> >> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> >> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
> >>  "%Y %m %d %H")
> >> a =  dta$date
> >> and obtained:
> >> > a
> >>  [1] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
> >>  [3] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
> >>  [5] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
> >>  [7] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
> >>  [9] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
> >> [11] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
> >> [13] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
> >> [15] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
> >> [17] "1998-01-06 06:00:00 GMT"
> >>
> >> Instead of getting AEST as in your own result, mine remains GMT.
> >>
> >> I think the problem is coming from my system or location, I am not
> >sure.
> >>
> >> Please have a look again and advise further.
> >>
> >> Thank you.
> >>
> >> On Wed, Jan 8, 2020 at 11:05 AM Jim Lemon <[hidden email]>
> >wrote:
> >> >
> >> > Hi again,
> >> > Small typo, should be
> >> >
> >> > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
> >> >  "%Y %m %d %H"
> >> >
> >> > as I tried it both with and without the century just to check and
> >> > copied the wrong line.
> >> >
> >> > On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <[hidden email]>
> >wrote:
> >> > >
> >> > > Hi Ogbos,
> >> > > I get the correct result using strptime:
> >> > >
> >> > > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
> >> > >  "%y %m %d %H"
> >> > > )
> >> > > > dta$date
> >> > > [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
> >> > > [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
> >> > > [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
> >> > > [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
> >> > > [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
> >> > > [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
> >> > > [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
> >> > > [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
> >> > > [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
> >> > > [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
> >> > > [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
> >> > > [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
> >> > > [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
> >> > > [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
> >> > > [29] "1998-05-02 06:00:00 AEST"
> >> > >
> >> > > Same result with
> >> > >
> >> > > ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
> >> > >
> >as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
> >> > >
> >> > > As it should be as the lower two call strptime. I can't see from
> >your
> >> > > code why the month and day are transcribed.
> >> > >
> >> > > Jim
> >
> >______________________________________________
> >[hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >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.
>
> --
> Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Jeff Newmiller
a) R cannot deal with POSIXt vectors having more than one timezone in one vector. Won't happen. You need to separate the data into different data frames if you need to deal with importing data with different timezones.

b) You say you included the  lines

Sys.timezone()
str(OlsonNames())

in your R code... but these are both used for you to extract information that could be useful to you in understanding how things are set up... they do NOT alter how R will do anything. To do that you would need to do something like

Sys.setenv( TZ="Africa/Lagos" )

and then convert some character data to POSIXct like

dta$timestamp <- as.POSIXct( dta$timestring, format=...


On January 8, 2020 8:55:19 AM PST, Ogbos Okike <[hidden email]> wrote:

>Dear Jeff,
>Thank you very much for looking into this.
>
>I have made some search on ?OlsonNames.
>System time zones indicates Africa/Lagos whereas the data I am trying
>to convert are Cosmic ray data prepared at different time zones
>including Oulu station (Russia), Climax station (America), many parts
>of Europe, etc.
>
>Including:
> Sys.timezone()
>str(OlsonNames())
>
>in my code gives:
>[1] "1998-01-05 02:00:00 WAT" "1998-01-05 03:00:00 WAT"
> [3] "1998-01-05 04:00:00 WAT" "1998-01-05 05:00:00 WAT"
> [5] "1998-01-05 06:00:00 WAT" "1998-01-05 07:00:00 WAT"
> [7] "1998-01-05 08:00:00 WAT" "1998-01-05 09:00:00 WAT"
> [9] "1998-01-05 10:00:00 WAT" "1998-01-05 11:00:00 WAT"
>[11] "1998-01-05 12:00:00 WAT" "1998-01-05 13:00:00 WAT"
>[13] "1998-01-05 14:00:00 WAT" "1998-01-05 15:00:00 WAT"
>[15] "1998-01-05 16:00:00 WAT" "1998-01-05 17:00:00 WAT"
>[17] "1998-01-05 18:00:00 WAT" "1998-01-05 19:00:00 WAT"
>[19] "1998-01-05 20:00:00 WAT" "1998-01-05 21:00:00 WAT"
>[21] "1998-01-05 22:00:00 WAT" "1998-01-05 23:00:00 WAT"
>[23] "1998-01-06 00:00:00 WAT" "1998-01-06 01:00:00 WAT"
>[25] "1998-01-06 02:00:00 WAT" "1998-01-06 03:00:00 WAT"
>[27] "1998-01-06 04:00:00 WAT" "1998-01-06 05:00:00 WAT"
>[29] "1998-01-06 06:00:00 WAT"
>
>Replacing the two lines above with:
>Sys.setenv( TZ="" )
>gives:
>[1] "1998-01-05 02:00:00 UTC" "1998-01-05 03:00:00 UTC"
> [3] "1998-01-05 04:00:00 UTC" "1998-01-05 05:00:00 UTC"
> [5] "1998-01-05 06:00:00 UTC" "1998-01-05 07:00:00 UTC"
> [7] "1998-01-05 08:00:00 UTC" "1998-01-05 09:00:00 UTC"
> [9] "1998-01-05 10:00:00 UTC" "1998-01-05 11:00:00 UTC"
>[11] "1998-01-05 12:00:00 UTC" "1998-01-05 13:00:00 UTC"
>[13] "1998-01-05 14:00:00 UTC" "1998-01-05 15:00:00 UTC"
>[15] "1998-01-05 16:00:00 UTC" "1998-01-05 17:00:00 UTC"
>[17] "1998-01-05 18:00:00 UTC" "1998-01-05 19:00:00 UTC"
>[19] "1998-01-05 20:00:00 UTC" "1998-01-05 21:00:00 UTC"
>[21] "1998-01-05 22:00:00 UTC" "1998-01-05 23:00:00 UTC"
>[23] "1998-01-06 00:00:00 UTC" "1998-01-06 01:00:00 UTC"
>[25] "1998-01-06 02:00:00 UTC" "1998-01-06 03:00:00 UTC"
>[27] "1998-01-06 04:00:00 UTC" "1998-01-06 05:00:00 UTC"
>[29] "1998-01-06 06:00:00 UTC"
>
>All these are still not correct and I am yet longing for further help.
>
>Best regards
>Ogbos
>
>On Wed, Jan 8, 2020 at 4:22 PM Jeff Newmiller
><[hidden email]> wrote:
>>
>> In your first email you said you were using
>>
>> Sys.setenv( TZ="GMT" )
>>
>> in your code, which defines the default assumption for time
>conversion timezone (at least until you change it). Keep in mind that
>you may be dealing with data from other timezones than your local one
>that the operating system uses, so the OS timezone only gets used if TZ
>is blank (and even that behavior can be OS-dependent I think).
>>
>> Read
>>
>> ?OlsonNames
>>
>> because AEST may not be a valid specification for TZ.
>>
>> On January 8, 2020 4:09:01 AM PST, Ogbos Okike
><[hidden email]> wrote:
>> >Dear Jim,
>> >In order to check whether I have a correct time on my system, I run:
>> >$ timedatectl
>> >
>> > and it gives:
>> >Local time: Wed 2020-01-08 13:03:44 WAT
>> >                  Universal time: Wed 2020-01-08 12:03:44 UTC
>> >                        RTC time: Wed 2020-01-08 12:03:44
>> >                       Time zone: Africa/Lagos (WAT, +0100)
>> >       System clock synchronized: yes
>> >systemd-timesyncd.service active: yes
>> >                 RTC in local TZ: no.
>> >
>> >The time is correct as it agrees with the system time.
>> >
>> >I don't know if there are other things I need to change.
>> >
>> >Thank you for any suggestions.
>> >Ogbos
>> >
>> >On Wed, Jan 8, 2020 at 12:55 PM Ogbos Okike
><[hidden email]>
>> >wrote:
>> >>
>> >> Dear Jim,
>> >> Thank you for coming my assist me.
>> >> I have tried all you suggested but the same result keep coming.
>> >> I tried, for example:
>> >> dta <- read.table("Ohr1may98", col.names = c("year", "month",
>"day",
>> >> "hour", "counts"))
>> >>
>> >> dta$year <- with( dta, ifelse(year < 50, year + 2000, year +
>1900))
>> >> dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >>  "%Y %m %d %H")
>> >> a =  dta$date
>> >> and obtained:
>> >> > a
>> >>  [1] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
>> >>  [3] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
>> >>  [5] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
>> >>  [7] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
>> >>  [9] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
>> >> [11] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
>> >> [13] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
>> >> [15] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
>> >> [17] "1998-01-06 06:00:00 GMT"
>> >>
>> >> Instead of getting AEST as in your own result, mine remains GMT.
>> >>
>> >> I think the problem is coming from my system or location, I am not
>> >sure.
>> >>
>> >> Please have a look again and advise further.
>> >>
>> >> Thank you.
>> >>
>> >> On Wed, Jan 8, 2020 at 11:05 AM Jim Lemon <[hidden email]>
>> >wrote:
>> >> >
>> >> > Hi again,
>> >> > Small typo, should be
>> >> >
>> >> > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >> >  "%Y %m %d %H"
>> >> >
>> >> > as I tried it both with and without the century just to check
>and
>> >> > copied the wrong line.
>> >> >
>> >> > On Wed, Jan 8, 2020 at 9:03 PM Jim Lemon <[hidden email]>
>> >wrote:
>> >> > >
>> >> > > Hi Ogbos,
>> >> > > I get the correct result using strptime:
>> >> > >
>> >> > > dta$date<-strptime(paste(dta$year,dta$month,dta$day,dta$hour),
>> >> > >  "%y %m %d %H"
>> >> > > )
>> >> > > > dta$date
>> >> > > [1] "1998-05-01 02:00:00 AEST" "1998-05-01 03:00:00 AEST"
>> >> > > [3] "1998-05-01 04:00:00 AEST" "1998-05-01 05:00:00 AEST"
>> >> > > [5] "1998-05-01 06:00:00 AEST" "1998-05-01 07:00:00 AEST"
>> >> > > [7] "1998-05-01 08:00:00 AEST" "1998-05-01 09:00:00 AEST"
>> >> > > [9] "1998-05-01 10:00:00 AEST" "1998-05-01 11:00:00 AEST"
>> >> > > [11] "1998-05-01 12:00:00 AEST" "1998-05-01 13:00:00 AEST"
>> >> > > [13] "1998-05-01 14:00:00 AEST" "1998-05-01 15:00:00 AEST"
>> >> > > [15] "1998-05-01 16:00:00 AEST" "1998-05-01 17:00:00 AEST"
>> >> > > [17] "1998-05-01 18:00:00 AEST" "1998-05-01 19:00:00 AEST"
>> >> > > [19] "1998-05-01 20:00:00 AEST" "1998-05-01 21:00:00 AEST"
>> >> > > [21] "1998-05-01 22:00:00 AEST" "1998-05-01 23:00:00 AEST"
>> >> > > [23] "1998-05-02 00:00:00 AEST" "1998-05-02 01:00:00 AEST"
>> >> > > [25] "1998-05-02 02:00:00 AEST" "1998-05-02 03:00:00 AEST"
>> >> > > [27] "1998-05-02 04:00:00 AEST" "1998-05-02 05:00:00 AEST"
>> >> > > [29] "1998-05-02 06:00:00 AEST"
>> >> > >
>> >> > > Same result with
>> >> > >
>> >> > > ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT")
>> >> > >
>>
>>as.POSIXct(ISOdatetime(dta$year,dta$month,dta$day,dta$hour,0,0,tz="GMT"))
>> >> > >
>> >> > > As it should be as the lower two call strptime. I can't see
>from
>> >your
>> >> > > code why the month and day are transcribed.
>> >> > >
>> >> > > Jim
>> >
>> >______________________________________________
>> >[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> >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.
>>
>> --
>> Sent from my phone. Please excuse my brevity.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Enrico Schumann-2
In reply to this post by Ogbos
>>>>> On Wed, 8 Jan 2020 17:43:29 +0100, Ogbos Okike <[hidden email]> writes:

  Ogbos> Dear Enrico,
  Ogbos> Thanks for your time.
  Ogbos> I have tried to learn how to use dput in R. I have not yet made much progress.

  Ogbos> I have succeeded in using dput to store my data frame. I first
  Ogbos> converted my data into a data frame and then used:
  Ogbos> dput(dd,file="Ogbos2",control = c("keepNA", "keepInteger",
  Ogbos> "showAttributes")) to output the dput file. dd is my data frame.

  Ogbos> When I opened the file, I didn't like its content as it differs very
  Ogbos> much from my data frame. But I don't know whether that makes sense to
  Ogbos> you. I am attaching the file.
  Ogbos> I am thanking you in advance for additional suggestions.
  Ogbos> Best wishes
  Ogbos> Ogbos

Hello Ogbos

your attempt worked fine: this is the data you sent


    dta <- structure(list(dta.year = c(98L, 98L, 98L, 98L, 98L, 98L, 98L,
      98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
      98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L), dta.month = c(1L,
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), dta.day = c(5L,
      5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
      5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), dta.hour = c(2L,
      3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
      17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L
      ), dta.counts = c(6462L, 6450L, 6423L, 6467L, 6480L, 6457L, 6417L,
      6467L, 6467L, 6468L, 6500L, 6482L, 6465L, 6465L, 6475L, 6452L,
      6440L, 6478L, 6470L, 6422L, 6448L, 6462L, 6485L, 6462L, 6485L,
      6470L, 6487L, 6515L, 6488L)), .Names = c("dta.year", "dta.month",
      "dta.day", "dta.hour", "dta.counts"), row.names = c(NA, -29L),
      class = "data.frame")

But then:

  head(dta)
  ##   dta.year dta.month dta.day dta.hour dta.counts
  ## 1       98         1       5        2       6462
  ## 2       98         1       5        3       6450
  ## 3       98         1       5        4       6423
  ## 4       98         1       5        5       6467
  ## 5       98         1       5        6       6480
  ## 6       98         1       5        7       6457

The data that you read in has January (1) as month.
So whatever goes wrong, seems to go wrong when you read
the data.  Are you quite sure you read the file you
read is the file you have shown?


kind regards
    Enrico

  Ogbos> On Wed, Jan 8, 2020 at 1:07 PM Enrico Schumann <[hidden email]> wrote:
  >>
  >>
  >> Quoting Ogbos Okike <[hidden email]>:
  >>
  >> > Dear Friends,
  >> > A sample of my data is:
  >> > 98 05 01 02    8541
  >> > 98 05 01 03    8548
  >> > 98 05 01 04    8512
  >> > 98 05 01 05    8541
  >> > 98 05 01 06    8509
  >> > 98 05 01 07    8472
  >> > 98 05 01 08    8454
  >> > 98 05 01 09    8461
  >> > 98 05 01 10    8462
  >> > 98 05 01 11    8475
  >> > 98 05 01 12    8433
  >> > 98 05 01 13    8479
  >> > 98 05 01 14    8417
  >> > 98 05 01 15    8463
  >> > 98 05 01 16    8473
  >> > 98 05 01 17    8450
  >> > 98 05 01 18    8433
  >> > 98 05 01 19    8437
  >> > 98 05 01 20    8437
  >> > 98 05 01 21    8438
  >> > 98 05 01 22    8421
  >> > 98 05 01 23    8420
  >> > 98 05 02 00    8371
  >> > 98 05 02 01    8338
  >> > 98 05 02 02    8251
  >> > 98 05 02 03    8204
  >> > 98 05 02 04    8183
  >> > 98 05 02 05    8231
  >> > 98 05 02 06    8242
  >> > Columns 1, 2, 3, 4 and 5 stands for year, month, day , hour and count.
  >> >
  >> > Using:
  >> > Sys.setenv( TZ="GMT" )
  >> >
  >> >
  >> > dta <- read.table("Ohr1may98", col.names = c("year", "month", "day",
  >> > "hour", "counts"))
  >> > dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
  >> > dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
  >> > a =  dta$datetime
  >> > I converted the datetime and plotted the graph of count vs a. The plot
  >> > was great but I have issues with the date.
  >> >
  >> > The raw data is for some hours for Ist and second day of may 1998 as
  >> > is evident from the sample data. But the result of date stored in "a"
  >> > above shows:
  >> >> a
  >> >  [1] "1998-01-05 02:00:00 GMT" "1998-01-05 03:00:00 GMT"
  >> >  [3] "1998-01-05 04:00:00 GMT" "1998-01-05 05:00:00 GMT"
  >> >  [5] "1998-01-05 06:00:00 GMT" "1998-01-05 07:00:00 GMT"
  >> >  [7] "1998-01-05 08:00:00 GMT" "1998-01-05 09:00:00 GMT"
  >> >  [9] "1998-01-05 10:00:00 GMT" "1998-01-05 11:00:00 GMT"
  >> > [11] "1998-01-05 12:00:00 GMT" "1998-01-05 13:00:00 GMT"
  >> > [13] "1998-01-05 14:00:00 GMT" "1998-01-05 15:00:00 GMT"
  >> > [15] "1998-01-05 16:00:00 GMT" "1998-01-05 17:00:00 GMT"
  >> > [17] "1998-01-05 18:00:00 GMT" "1998-01-05 19:00:00 GMT"
  >> > [19] "1998-01-05 20:00:00 GMT" "1998-01-05 21:00:00 GMT"
  >> > [21] "1998-01-05 22:00:00 GMT" "1998-01-05 23:00:00 GMT"
  >> > [23] "1998-01-06 00:00:00 GMT" "1998-01-06 01:00:00 GMT"
  >> > [25] "1998-01-06 02:00:00 GMT" "1998-01-06 03:00:00 GMT"
  >> > [27] "1998-01-06 04:00:00 GMT" "1998-01-06 05:00:00 GMT"
  >> > [29] "1998-01-06 06:00:00 GMT"
  >> > This seems to suggest day 5 and 6 in January 1998 instead of day 1 and
  >> > 2 in May of 1998.
  >> >
  >> > I have spent some time trying to resolve this but I have not been successful.
  >> >
  >> > I would be thankful if you could help me to check where I went astray.
  >> >
  >> > Thank you.
  >> > Best wishes
  >> > Ogbos
  >> >
  >>
  >> I cannot reproduce these results. Could you please provide a fully
  >> reproducible example, by providing a small example dataset via 'dput(dta)'?
  >>
  >>
  >> --
  >> Enrico Schumann
  >> Lucerne, Switzerland
  >> http://enricoschumann.net
  >>

  Ogbos> structure(list(dta.year = c(98L, 98L, 98L, 98L, 98L, 98L, 98L,
  Ogbos> 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L,
  Ogbos> 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L, 98L), dta.month = c(1L,
  Ogbos> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  Ogbos> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), dta.day = c(5L,
  Ogbos> 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  Ogbos> 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), dta.hour = c(2L,
  Ogbos> 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
  Ogbos> 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L
  Ogbos> ), dta.counts = c(6462L, 6450L, 6423L, 6467L, 6480L, 6457L, 6417L,
  Ogbos> 6467L, 6467L, 6468L, 6500L, 6482L, 6465L, 6465L, 6475L, 6452L,
  Ogbos> 6440L, 6478L, 6470L, 6422L, 6448L, 6462L, 6485L, 6462L, 6485L,
  Ogbos> 6470L, 6487L, 6515L, 6488L)), .Names = c("dta.year", "dta.month",
  Ogbos> "dta.day", "dta.hour", "dta.counts"), row.names = c(NA, -29L), class = "data.frame")


--
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect Conversion of Datetime

Jim Lemon-4
Enrico seems to have found the problem - you can't change dates much
by changing the time zone, but transposing month and day will do the
trick nicely. The mm/dd/yyyy format and others like it are so common
that you always have to be on the lookout for it. Is the sample data
you included in your first email taken from the file you are reading
in? When I read in that sample data the correct result was returned. I
would open the original file in a text editor, read the data in using
your commands and then:

head(dta)

and compare what appears with the original file. You may see the
problem straightaway.

Jim


On Thu, Jan 9, 2020 at 7:50 AM Enrico Schumann <[hidden email]> wrote:
> ...
> The data that you read in has January (1) as month.
> So whatever goes wrong, seems to go wrong when you read
> the data.  Are you quite sure you read the file you
> read is the file you have shown?
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Incorrect Conversion of Datetime: Fixed

Ogbos
Dear ALL,
I am really happy. Erinco has pointed out the error.

Out of the the 5 stations'  Cosmic ray data I am staking together to
show some similar effects, I wrongly (and I am sorry for taking your
time) copied one of the dates, including January 1, 1998 instead of
May.
Others were correct and it will never occur to me, except for the keen
eye of Erinco, that the problem is from any of the data.

This singular correction has also changed my plot and all the
attendant analysis.
Thank you all for your kind suggestions.
Warmest regards
Ogbos
On Wed, Jan 8, 2020 at 10:17 PM Jim Lemon <[hidden email]> wrote:

>
> Enrico seems to have found the problem - you can't change dates much
> by changing the time zone, but transposing month and day will do the
> trick nicely. The mm/dd/yyyy format and others like it are so common
> that you always have to be on the lookout for it. Is the sample data
> you included in your first email taken from the file you are reading
> in? When I read in that sample data the correct result was returned. I
> would open the original file in a text editor, read the data in using
> your commands and then:
>
> head(dta)
>
> and compare what appears with the original file. You may see the
> problem straightaway.
>
> Jim
>
>
> On Thu, Jan 9, 2020 at 7:50 AM Enrico Schumann <[hidden email]> wrote:
> > ...
> > The data that you read in has January (1) as month.
> > So whatever goes wrong, seems to go wrong when you read
> > the data.  Are you quite sure you read the file you
> > read is the file you have shown?
> >

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.