Skip to content

strftime - Dates from Excel files

4 messages · Ana, Brian Ripley, Mikko Korpela +1 more

Ana
#
Hi

I have many excel files were the Date field was not declared as date,
so the dates look like this: 1/2/1978
I know that the format is day/month/year

How can I make R change this to Date format?

If I use strftime, I get wrong dates:

dataset=c("1/2/1978")

strftime(dataset,"%d/%m/%Y")
"19/02/0001"


Thanks in advance.
#
On Fri, 3 Feb 2012, Ana wrote:

            
So use as.Date to convert to the Date class.
[1] "1978-02-01"
On some unstated OS (how year 1 is represented is OS-dependent). 
The format in strftime applies to output: the default one is used for 
input.
#
On 02/03/2012 03:34 PM, Ana wrote:
Hi!

Prof. Ripley already provided a nice, concise answer, but here's a more
verbose one.

The function strftime() is used for output formatting. In your example,
"%d/%m/%Y" is the chosen output format. Use strptime() for converting
character vectors (i.e. text input) to class "POSIXlt". For converting
to class "Date", use as.Date(). These are alternative classes for
representing dates in R.
[1] "1978-02-01"
[1] "1978-02-01"

For converting "POSIXlt" or "Date" back to a character representation,
use as.character() or, for a customizable style, format().
[1] "Wed Feb 01, 1978"
1 day later
#
I think you are using the wrong function. See ?strftime

Try
dataset=c("1/2/1978")
strptime(dataset,"%d/%m/%Y")

John Kane
Kingston ON Canada
____________________________________________________________
Send any screenshot to your friends in seconds...
Works in all emails, instant messengers, blogs, forums and social networks.
TRY IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if2 for FREE