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:> 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:So use as.Date to convert to the Date class.> as.Date(dataset,"%d/%m/%Y")[1] "1978-02-01"> > dataset=c("1/2/1978") > > strftime(dataset,"%d/%m/%Y") > "19/02/0001"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. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 02/03/2012 03:34 PM, Ana wrote:> 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"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.> strptime(dataset, format="%d/%m/%Y")[1] "1978-02-01"> as.Date(dataset, format="%d/%m/%Y")[1] "1978-02-01" For converting "POSIXlt" or "Date" back to a character representation, use as.character() or, for a customizable style, format().> format(strptime(dataset, format="%d/%m/%Y"), "%a %b %d, %Y")[1] "Wed Feb 01, 1978" -- Mikko Korpela Aalto University School of Science Department of Information and Computer Science
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> -----Original Message----- > From: rrasterr at gmail.com > Sent: Fri, 3 Feb 2012 14:34:09 +0100 > To: r-help at r-project.org > Subject: [R] strftime - Dates from Excel files > > 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. > > ______________________________________________ > 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.____________________________________________________________ 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