Hi, I imported an excel table (using read.csv) of Dow Jones monthly average closings where the first variable is a date as a character string such as "01OCT1928". How do I convert this to a date variable so I can plot monthly average closings against date using ggplot2? Thanks, Walt ________________________ Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 ________________________ (V) 609-936-8999 (F) 609-936-3733 walt at dataanalyticscorp.com www.dataanalyticscorp.com
On Jun 19, 2012, at 8:00 AM, Data Analytics Corp. wrote:> Hi, > > I imported an excel table (using read.csv) of Dow Jones monthly > average closings where the first variable is a date as a character > string such as "01OCT1928". How do I convert this to a date > variable so I can plot monthly average closings against date using > ggplot2??strptime # has the format specs that you can use with ... ?as.Date -- David Winsemius, MD West Hartford, CT
Hi Walt, as.Date("01OCT1928", "%d%b%Y") works for me. See also ?strftime Regards, Jon On Tue, Jun 19, 2012 at 8:00 PM, Data Analytics Corp. <walt at dataanalyticscorp.com> wrote:> Hi, > > I imported an excel table (using read.csv) of Dow Jones monthly average > closings where the first variable is a date as a character string such as > "01OCT1928". ?How do I convert this to a date variable so I can plot monthly > average closings against date using ggplot2? > > Thanks, > > Walt > > ________________________ > > Walter R. Paczkowski, Ph.D. > Data Analytics Corp. > 44 Hamilton Lane > Plainsboro, NJ 08536 > ________________________ > (V) 609-936-8999 > (F) 609-936-3733 > walt at dataanalyticscorp.com > www.dataanalyticscorp.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.
Hi, Try this: ?str1<-c("01OCT1928","02OCT1928","03OCT1928","04OCT1928") strptime(str1,"%d%b%Y") [1] "1928-10-01" "1928-10-02" "1928-10-03" "1928-10-04" #or you can use this: ?x<-as.Date(str1,format="%d%b%Y") ?x [1] "1928-10-01" "1928-10-02" "1928-10-03" "1928-10-04" A.K. ----- Original Message ----- From: Data Analytics Corp. <walt at dataanalyticscorp.com> To: r-help at r-project.org Cc: Sent: Tuesday, June 19, 2012 8:00 AM Subject: [R] Date formats Hi, I imported an excel table (using read.csv) of Dow Jones monthly average closings where the first variable is a date as a character string such as "01OCT1928".? How do I convert this to a date variable so I can plot monthly average closings against date using ggplot2? Thanks, Walt ________________________ Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 ________________________ (V) 609-936-8999 (F) 609-936-3733 walt at dataanalyticscorp.com www.dataanalyticscorp.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.
Hello, Try as.Date("01OCT1928", format="%d%b%Y") [1] "1928-10-01" Note that though probably not a problem to you, this is locale specific. In Portugal, the string corresponding to the same date would be "01OUT1928". The variable to set using Sys.setlocale() would be LC_TIME. For date formats the help page you want is help("strptime") ?strptime Hope this helps, Rui Barradas Em 19-06-2012 13:00, Data Analytics Corp. escreveu:> Hi, > > I imported an excel table (using read.csv) of Dow Jones monthly average > closings where the first variable is a date as a character string such > as "01OCT1928". How do I convert this to a date variable so I can plot > monthly average closings against date using ggplot2? > > Thanks, > > Walt > > ________________________ > > Walter R. Paczkowski, Ph.D. > Data Analytics Corp. > 44 Hamilton Lane > Plainsboro, NJ 08536 > ________________________ > (V) 609-936-8999 > (F) 609-936-3733 > walt at dataanalyticscorp.com > www.dataanalyticscorp.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.
Hi, Since you need the monthly averages, I thought it might be helpful to have the month and the year alone in the plot when compared to the whole date. Try this: str2<-c("01OCT1928","01NOV1928","01DEC1928","01JAN1929") x1<-as.Date(str2,format="%d%b%Y") library(zoo)as.yearmon(x1) [1] "Oct 1928" "Nov 1928" "Dec 1928" "Jan 1929" A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: "walt at dataanalyticscorp.com" <walt at dataanalyticscorp.com> Cc: R help <r-help at r-project.org> Sent: Tuesday, June 19, 2012 7:04 PM Subject: Re: [R] Date formats Hi, Try this: ?str1<-c("01OCT1928","02OCT1928","03OCT1928","04OCT1928") strptime(str1,"%d%b%Y") [1] "1928-10-01" "1928-10-02" "1928-10-03" "1928-10-04" #or you can use this: ?x<-as.Date(str1,format="%d%b%Y") ?x [1] "1928-10-01" "1928-10-02" "1928-10-03" "1928-10-04" A.K. ----- Original Message ----- From: Data Analytics Corp. <walt at dataanalyticscorp.com> To: r-help at r-project.org Cc: Sent: Tuesday, June 19, 2012 8:00 AM Subject: [R] Date formats Hi, I imported an excel table (using read.csv) of Dow Jones monthly average closings where the first variable is a date as a character string such as "01OCT1928".? How do I convert this to a date variable so I can plot monthly average closings against date using ggplot2? Thanks, Walt ________________________ Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 ________________________ (V) 609-936-8999 (F) 609-936-3733 walt at dataanalyticscorp.com www.dataanalyticscorp.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.