On Wed, 6 Jan 2010, Larry White wrote:
Hi, I'm reading data from a text file and transforming it in R and my date
column seems to be getting corrupted. Can someone point out what's wrong?
This code worked fine until I added a new date in 2010.
Seems unlikely, but we don't have a reproducible example. If working
as documented you would have got the wrong answer in 2009 also.
thank you.
To load the data I run:
work_table = read.table(datafilename,header=TRUE) #read the
data file
attach(work_table) #attach
the file for ease of use
names(work_table) =
c( "cur_date", "week", "time_pct", "compl", "work_delta",
"mean_delta", "balance", "total", "total_delta",
"work", "index", "mean_pd_per_day")
If I list the date element cur_date, I get:
work_table$cur_date
[1] 8/17/2009 8/30/2009 9/6/2009 9/13/2009 9/20/2009 9/27/2009
[7] 10/4/2009 10/13/2009 10/20/2009 10/27/2009 11/3/2009 11/10/2009
[13] 11/17/2009 11/24/2009 12/2/2009 12/9/2009 12/16/2009 12/23/2009
[19] 12/30/2009 1/6/2010
This is correct.
The line that produces the bad output is:
dt <-strptime(as.character(work_table$cur_date), "%m/%d/%y")
when I list dt I get:
dt
[1] "2020-08-17" "2020-08-30" "2020-09-06" "2020-09-13" "2020-09-20"
[6] "2020-09-27" "2020-10-04" "2020-10-13" "2020-10-20" "2020-10-27"
[11] "2020-11-03" "2020-11-10" "2020-11-17" "2020-11-24" "2020-12-02"
[16] "2020-12-09" "2020-12-16" "2020-12-23" "2020-12-30" "2020-01-06"
The inside function as.character seems to provide correct output
as.character(work_table$cur_date)
[1] "8/17/2009" "8/30/2009" "9/6/2009" "9/13/2009" "9/20/2009"
[6] "9/27/2009" "10/4/2009" "10/13/2009" "10/20/2009" "10/27/2009"
[11] "11/3/2009" "11/10/2009" "11/17/2009" "11/24/2009" "12/2/2009"
[16] "12/9/2009" "12/16/2009" "12/23/2009" "12/30/2009" "1/6/2010"
which makes me think I'm doing something wrong in my strptime() call.
So look at the help page, and compare %y and %Y ...?
as.character(bug_table$rep_date)
Thanks very much for your help
[[alternative HTML version deleted]]
Please note you were asked not to send HTML. Also, strptime() is an
OS function, so your OS might well have been relevant (see the posting
guide).
--
Brian D. Ripley, rip...@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
______________________________________________
R-help@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.