Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting year and month ("yyyy-mm" format) to a date?

Tags:

date

posix

r

r-faq

zoo

I have a dataset that looks like this:

Month    count 2009-01  12 2009-02  310 2009-03  2379 2009-04  234 2009-05  14 2009-08  1 2009-09  34 2009-10  2386 

I want to plot the data (months as x values and counts as y values). Since there are gaps in the data, I want to convert the Information for the Month into a date. I tried:

as.Date("2009-03", "%Y-%m") 

But it did not work. Whats wrong? It seems that as.Date() requires also a day and is not able to set a standard value for the day? Which function solves my problem?

like image 745
R_User Avatar asked Jun 05 '11 12:06

R_User


People also ask

How do you convert YYYY MM to date?

In Excel, if you want to convert date to text with yyyy-mm-dd format, you can use formula. 1. Select a blank cell next to your date, for instance. I1, and type this formula =TEXT(G1, "yyyy-mm-dd"), and press Enter key, then drag AutoFill handle over the cells needed this formula.

How do you convert YYYY MM to MM YYYY in Excel?

How do I change from mm/dd/yyyy to mm, dd, and yyyyy in Excel? Stay on the cell where you want to change the format, then click on Ctrl + 1. This takes you to the Format cells dialog box. Now from the top tabs click on the Number tab and then select custom (last option).


2 Answers

Since dates correspond to a numeric value and a starting date, you indeed need the day. If you really need your data to be in Date format, you can just fix the day to the first of each month manually by pasting it to the date:

month <- "2009-03" as.Date(paste(month,"-01",sep="")) 
like image 93
Sacha Epskamp Avatar answered Sep 27 '22 19:09

Sacha Epskamp


Try this. (Here we use text=Lines to keep the example self contained but in reality we would replace it with the file name.)

Lines <- "2009-01  12 2009-02  310 2009-03  2379 2009-04  234 2009-05  14 2009-08  1 2009-09  34 2009-10  2386"  library(zoo) z <- read.zoo(text = Lines, FUN = as.yearmon) plot(z) 

The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of ?plot.zoo .

The zoo series, z, that is created above has a "yearmon" time index and looks like this:

> z Jan 2009 Feb 2009 Mar 2009 Apr 2009 May 2009 Aug 2009 Sep 2009 Oct 2009        12      310     2379      234       14        1       34     2386  

"yearmon" can be used alone as well:

> as.yearmon("2000-03") [1] "Mar 2000" 

Note:

  1. "yearmon" class objects sort in calendar order.

  2. This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of z to "Date" class: time(z) <- as.Date(time(z)) .

like image 36
G. Grothendieck Avatar answered Sep 27 '22 19:09

G. Grothendieck