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 -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
On Tue, Jun 26, 2012 at 10:54 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:> 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")Right about ........... ^ should be a percent instead of a dollar sign. Also, probably can't hurt to used a named argument (but I don't think that's the problem here) In the future dput()-ery would be much appreciated. Michael> [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 > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > 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.
On Tue, 26-Jun-2012 at 10:54PM -0500, Erin Hodgess wrote: |> 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. I don't think that's the reason why.> as.Date(c("1/2/2011", "1/4/2011", "1/4/2011"), format = "%m/%d/%Y")[1] "2011-01-02" "2011-01-04" "2011-01-04">Leading zeros aren't essential. |> |> There are approximately 30 years of nearly daily data in the entire set. |> |> Any suggestions would be much appreciated. |> |> Sincerely, |> Erin |> |> |> -- |> Erin Hodgess |> Associate Professor |> Department of Computer and Mathematical Sciences |> University of Houston - Downtown |> mailto: erinm.hodgess at gmail.com |> |> ______________________________________________ |> 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. -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Average minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Eleanor Roosevelt ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
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 > >
Hi, I can see the mistake in the code as "$" before Y.? ?dat1<-as.factor(c("1/2/2011","1/4/2011","1/4/2011")) dat1 [1] 1/2/2011 1/4/2011 1/4/2011 Levels: 1/2/2011 1/4/2011 ?as.Date(as.character(dat1), "%m/%d/%Y") [1] "2011-01-02" "2011-01-04" "2011-01-04"> as.Date(as.character(dat1),"%m/%d/$Y")[1] NA NA NA A.K. ----- Original Message ----- From: Erin Hodgess <erinm.hodgess at gmail.com> To: R help <r-help at stat.math.ethz.ch> Cc: Sent: Tuesday, June 26, 2012 11:54 PM Subject: [R] question about formatting Dates 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 -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com ______________________________________________ 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.
How about> dates <- as.factor(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"))> dates[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> format(as.Date(dates, "%m/%d/%Y"), "%m/%d/%Y")[1] "01/02/2011" "01/04/2011" "01/04/2011" "01/04/2011" "01/06/2011" [6] "01/07/2011" "01/08/2011" "01/09/2011" "01/10/2011" ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Erin Hodgess > Sent: Tuesday, June 26, 2012 10:54 PM > To: R help > Subject: [R] question about formatting Dates > > 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 > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > 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.