why is as.date function not working for me? (dd/mm/yyyy h:mm)

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

why is as.date function not working for me? (dd/mm/yyyy h:mm)

jcrosbie
Why am I know getting hours after I convert the date?

 dates <- c('31/12/2013 0:00',  '31/12/2013 1:00',  '31/12/2013 2:00',  '31/12/2013 3:00',  '31/12/2013 4:00',  '31/12/2013 5:00',  '31/12/2013 6:00',  '31/12/2013 7:00',  '31/12/2013 8:00',  '31/12/2013 9:00', '31/12/2013 10:00', '31/12/2013 11:00',  '31/12/2013 12:00', '31/12/2013 13:00', '31/12/2013 14:00', '31/12/2013 15:00',  '31/12/2013 16:00', '31/12/2013 17:00', '31/12/2013 18:00', '31/12/2013 19:00', '31/12/2013 20:00', '31/12/2013 21:00', '31/12/2013 22:00', '31/12/2013 23:00',  '01/01/2014 0:00')

as.Date(dates, format="%d/%m/%Y %H:%M")
 [1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
 [6] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
[11] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
[16] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
[21] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2014-01-01"

Reply | Threaded
Open this post in threaded view
|

Re: why is as.date function not working for me? (dd/mm/yyyy h:mm)

Luke Miller
as.Date produces Dates only, with no time information, even if you try
to supply it with hours + minutes.

For dates+times, use as.POSIXct() or as.POSIXlt() in place of
as.Date(). POSIXct produces a numeric value for the number of seconds
since your specified origin time (usually 1970-01-01 00:00), and
POSIXlt produces a list object with individual entries for year,
month, day, hour, minute etc. Be aware that time zones and daylight
savings transitions may suddenly come into play. See ?DateTimeClasses
for the gory details.

as.POSIXct(dates, format = "%d/%m/%Y %H:%M")


On Thu, Feb 6, 2014 at 10:24 AM, jcrosbie <[hidden email]> wrote:

> Why am I know getting hours after I convert the date?
>
>  dates <- c('31/12/2013 0:00',  '31/12/2013 1:00',  '31/12/2013 2:00',
> '31/12/2013 3:00',  '31/12/2013 4:00',  '31/12/2013 5:00',  '31/12/2013
> 6:00',  '31/12/2013 7:00',  '31/12/2013 8:00',  '31/12/2013 9:00',
> '31/12/2013 10:00', '31/12/2013 11:00',  '31/12/2013 12:00', '31/12/2013
> 13:00', '31/12/2013 14:00', '31/12/2013 15:00',  '31/12/2013 16:00',
> '31/12/2013 17:00', '31/12/2013 18:00', '31/12/2013 19:00', '31/12/2013
> 20:00', '31/12/2013 21:00', '31/12/2013 22:00', '31/12/2013 23:00',
> '01/01/2014 0:00')
>
> as.Date(dates, format="%d/%m/%Y %H:%M")
>  [1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
>  [6] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
> [11] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
> [16] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
> [21] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2014-01-01"
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/why-is-as-date-function-not-working-for-me-dd-mm-yyyy-h-mm-tp4684874.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: why is as.date function not working for me? (dd/mm/yyyy h:mm)

MacQueen, Don
In reply to this post by jcrosbie
Because a Date object represents calendar dates, and calendar dates don't
have hours.

Use as.POSIXct() instead of as.Date()
(and spend a little more time with the documentation for as.Date)

-Don

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 2/6/14 10:24 AM, "jcrosbie" <[hidden email]> wrote:

>Why am I know getting hours after I convert the date?
>
> dates <- c('31/12/2013 0:00',  '31/12/2013 1:00',  '31/12/2013 2:00',
>'31/12/2013 3:00',  '31/12/2013 4:00',  '31/12/2013 5:00',  '31/12/2013
>6:00',  '31/12/2013 7:00',  '31/12/2013 8:00',  '31/12/2013 9:00',
>'31/12/2013 10:00', '31/12/2013 11:00',  '31/12/2013 12:00', '31/12/2013
>13:00', '31/12/2013 14:00', '31/12/2013 15:00',  '31/12/2013 16:00',
>'31/12/2013 17:00', '31/12/2013 18:00', '31/12/2013 19:00', '31/12/2013
>20:00', '31/12/2013 21:00', '31/12/2013 22:00', '31/12/2013 23:00',
>'01/01/2014 0:00')
>
>as.Date(dates, format="%d/%m/%Y %H:%M")
> [1] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
> [6] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
>[11] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
>[16] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31"
>[21] "2013-12-31" "2013-12-31" "2013-12-31" "2013-12-31" "2014-01-01"
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/why-is-as-date-function-not-working-for-me-d
>d-mm-yyyy-h-mm-tp4684874.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: why is as.date function not working for me? (dd/mm/yyyy h:mm)

jcrosbie
Thanks. as.POSIXct works for the most part. The only problem is part of data I'm working has it's own time zone. Is there a way to not have a time zone displayed? My times do not change with Daylight saving.
Reply | Threaded
Open this post in threaded view
|

Re: why is as.date function not working for me? (dd/mm/yyyy h:mm)

jcrosbie
This function returns date/times without timezone strptime(dates,format="%d/%m/%Y %H:%M")

Reply | Threaded
Open this post in threaded view
|

Re: why is as.date function not working for me? (dd/mm/yyyy h:mm)

Jeff Newmiller
You must deal with identifying the time zone. I have found that setting TZ environment variable appropriately for the data before converting character values to POSIXct gives me the best results. This is actually easier for standard-time-only data than for data with daylight savings time transitions. For example I use

Sys.setenv(TZ="Etc/GMT+8")

for year-round Standard time in California.

The chron library offers an alternative approach that intentionally ignores time zones, but I have not found it so useful for my purposes as POSIXct.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On February 6, 2014 1:30:22 PM PST, jcrosbie <[hidden email]> wrote:

>This function returns date/times without timezone
>strptime(dates,format="%d/%m/%Y %H:%M")
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/why-is-as-date-function-not-working-for-me-dd-mm-yyyy-h-mm-tp4684874p4684895.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.