[R] Reading a datetime vector

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Fri Feb 19 18:41:48 CET 2016


This is a mailing list. I don't know how you are interacting with it... using a website rather than an email program can lead to some confusion since there can be many ways to accomplish the task of interacting with the mailing list. My email program has a "reply-all" button when I am looking at an email. It also has an option to write the email in plain text, which often prevents the message from getting corrupted (recipient not seeing what you sent to the list).

Using the str function on a literal string (the name of a file) will indeed tell you that you gave it a character string. Specifying a column in your data might tell you something more interesting... e.g.

str( df2_TZ$DateTimeStamp )

If that says you have character data then Jim Lemon's suggestion would be a good next thing to look at.  If it is factor data then you should use the as.character function on the data column and then follow Jim's suggestion. If it is numeric then you probably need to convert it using an appropriate origin (e.g. as described at [1] or [2]).

I have had best luck setting the default timezone string when converting to POSIXt types... e.g.

# specify timezone assumed by input data
Sys.setenv( TZ="GMT" )
testdtm <- as.POSIXct( "1/1/2016 00:00", format = "%m/%d/%Y %H:%M" )
# inspect the result
testdtm
str( testdtm )
# view data from a different timezone
Sys.setenv( TZ="Etc/GMT+8" )
# no change to the underlying data, but it prints out differently now because the tz attribute is "" which implies using the default TZ
testdtm

[1] http://blog.mollietaylor.com/2013/08/date-formats-in-r.html
[2] https://www.r-project.org/doc/Rnews/Rnews_2004-1.pdf

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

On February 19, 2016 7:48:31 AM PST, D Wolf <doug45290 at yahoo.com> wrote:
>Hello Jeff,
>I ran str() on the vector and it returned character.>
>str("DF_exp.xlsx") chr "DF_exp.xlsx"
>This is my first thread on this forum, and I'm not sure how to reply to
>the thread instead of just sending the reply to your email account; I
>don't see a 'reply' link in the thread.I've read this page and I don't
>think it advises on how to reply in the thread: R: Posting Guide: How
>to ask good questions that prompt useful answers
>
>|   |
>|   |  |   |   |   |   |   |
>| R: Posting Guide: How to ask good questions that prompt ...Posting
>Guide: How to ask good questions that prompt useful answers This guide
>is intended to help you get the most out of the R mailing lists, and to
>avoid embarra... |
>|  |
>| View on www.r-project.org | Preview by Yahoo |
>|  |
>|   |
>
>
>Thank You,Doug Wolfinger
> 
>
>On Friday, February 19, 2016 12:51 AM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us> wrote:
> 
>
>You are being rather scattershot in your explanation, so I suspect you
>are not being systematic in your troubleshooting. Use the str function
>to examine the data column after you pull it in from excel. It may be
>numeric, factor, or character, and the approach depends on which that
>function returns. 
>-- 
>Sent from my phone. Please excuse my brevity.
>
>On February 18, 2016 1:12:40 PM PST, D Wolf via R-help
><r-help at r-project.org> wrote:
>Hello,I am trying to read a data frame column named DateTimeStamp. The
>time is in GMT in this format: 1/4/2013 23:30
>require(xlsx)
>df2_TZ = read.xlsx2("DF_exp.xlsx", sheetName = "Sheet1")
>
>It's good to that line. But these three lines, which makes the
>dataframe, converts the column's values to NA:df2_TZ$DateTimeStamp =
>as.POSIXct(df2_TZ$DateTimeStamp, format="%m/%d/%Y %H:%M:%S", tz="GMT")
>
>and... df2_TZ$DateTimeStamp =
>as.POSIXct(as.character(df2_TZ$DateTimeStamp), format = "%m/%d/%Y
>%H:%M:%S")
>
>and...df2_TZ$DateTimeStamp = as.Date(df2_TZ$DateTimeStamp, format =
>"%m/%d/%Y %H:%M:%S")
>
>This line returns and error...df2_TZ$DateTimeStamp =
>as.POSIXct(as.Date(df2_TZ$DateTimeStamp), format = "%m/%d/%Y %H:%M:%S")
>"Error in charToDate(x) :   character string is not in a standard
>unambiguous format"
>Additionally, I need to convert from GMT to North American time zones,
>and I think the advice on this page would
>be good for
>that: http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html
>My ultimate goal is to write an R program that finds data in another
>variable in df2_TZ that corresponds to a date and time that match up
>with the date and time in another data frame. For now, any help reading
>the column would be much appreciated.
>Thank You,Doug
> [[alternative HTML version deleted]]
>
>
>R-help at r-project.org 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list