Hello,

I'm afraid you're wrong, this has nothing to do with leading zeros. Just see:

x <- c("1/2/2011", "1/4/2011", "1/4/2011", "1/4/2011", "1/6/2011", "1/7/2011",
        "1/8/2011",  "1/9/2011",  "1/10/2011")
as.Date(x, "%m/%d/%Y")

y <- factor(x)
str(y)
as.Date(as.character(y), "%m/%d/%Y")


Note that the correct way of transforming factors into their levels is to index the levels by the factor's values (which are always positive integers):

levels(y)[y]
[1] "1/2/2011"  "1/4/2011"  "1/4/2011"  "1/4/2011"  "1/6/2011"  "1/7/2011"
[7] "1/8/2011"  "1/9/2011"  "1/10/2011"


Also, if those dates come from a file, maybe you want to read.table or read.csv with the option

stringsAsFactors = FALSE

and avoid the problem altogether. This has the side effect that all character variables will be read in as character variables and some might be categorical. What an ugly sentence! So this option must be used with caution.

Hope this helps,

Rui Barradas

Em 27-06-2012 04:54, Erin Hodgess escreveu:
Dear R People:

I have dates as factors in the following:

poudel.df$DATE
[1] 1/2/2011  1/4/2011  1/4/2011  1/4/2011  1/6/2011  1/7/2011  1/8/2011
[8] 1/9/2011  1/10/2011
Levels: 1/10/2011 1/2/2011 1/4/2011 1/6/2011 1/7/2011 1/8/2011 1/9/2011



I want them to be "regular" dates which can be sorted, etc.

But when I did this:

as.character(poudel.df$DATE)
[1] "1/2/2011"  "1/4/2011"  "1/4/2011"  "1/4/2011"  "1/6/2011"  "1/7/2011"
[7] "1/8/2011"  "1/9/2011"  "1/10/2011"

and
as.Date(as.character(poudel.df$DATE),"%m/%d/$Y")
[1] NA NA NA NA NA NA NA NA NA

because the dates do not have leading zeros.

There are approximately 30 years of nearly daily data in the entire set.

Any suggestions would be much appreciated.

Sincerely,
Erin



______________________________________________
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.

Reply via email to