0

I am going crazy with the as.Date function in R. I just do not understand why my code does not work.

Here, you find the data:

irates_monthly = c("2000-01", "2000-02", "2000-03", "2000-04", "2000-05", "2000-06", "2000-07", "2000-08", "2000-09", "2000-10", "2000-11", "2000-12", "2001-01", "2001-02", "2001-03", "2001-04")

which is of type 'character'.

Now, here is my code:

as.Date(irates_monthly, format = '%Y-%m')

The only thing I want, is to transform the character vector 'irates_monthly' into a Date object such that I can use it in the xts() function with the argument 'order.by'.

I am sure this is very simple but I just do not see the mistake. Any help is very much appreciated! Also searching through stackoverflow did not resolve my problem!

I am very thankful for any hints, or solutions!

All the best,

Manuel

m0byn
  • 63
  • 7
  • Did you read [Converting year and month (“yyyy-mm” format) to a date?](https://stackoverflow.com/questions/6242955/converting-year-and-month-yyyy-mm-format-to-a-date) ? – markus Nov 20 '20 at 19:56

2 Answers2

1
library(zoo)
irates_monthly = c("2000-01", "2000-02", "2000-03", "2000-04", "2000-05", "2000-06", "2000-07", "2000-08", "2000-09", "2000-10", "2000-11", "2000-12", "2001-01", "2001-02", "2001-03", "2001-04")

dts <- as.yearmon(irates_monthly)
> dts
 [1] "Jan 2000" "Feb 2000" "Mar 2000" "Apr 2000" "May 2000" "Jun 2000" "Jul 2000" "Aug 2000" "Sep 2000" "Oct 2000" "Nov 2000" "Dec 2000" "Jan 2001" "Feb 2001"
[15] "Mar 2001" "Apr 2001"

dts2 <- as.Date(dts)
> dts2
 [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01"
[13] "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01"

If you don't want to use zoo, you can add the first day of a month such that "as.Date" works:

> as.Date(as.character(paste0(irates_monthly, '-01')))
 [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01"
[13] "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01"
tester
  • 1,662
  • 1
  • 10
  • 16
  • One question: does as.Date ALWAYS need year, month, and day? – m0byn Nov 20 '20 at 19:42
  • The correct answer to this question is "no", however, for daily applications, the answer is "yes". – tester Nov 20 '20 at 19:45
  • Well, I still do not understand the problem then. Why doesnt as.Date work only with year and month? – m0byn Nov 20 '20 at 20:01
  • `as.Date(12345)` will give you a result, and you didn't enter year + month + date. To convert a string like '2000-01-01' to a date, you will need all three of them. – tester Nov 21 '20 at 14:44
0

We could use ymd from lubridate with truncated

library(lubridate)
ymd(irates_monthly, truncated = 1)
#[1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01"
#[11] "2000-11-01" "2000-12-01" "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01"
akrun
  • 874,273
  • 37
  • 540
  • 662