0

I have following column which contains year and month but with a space between them:

date
2000 Oct
2000 Nov
2000 Dec
2001 Jan
2001 Feb
2001 Mar

[1] 2000 Oct 2000 Nov 2000 Dec 2001 Jan 2001 Feb 2001

I want to convert this to a date column

I tried following: as.Date(df$date, "%YYYY %mm") but it gave me all NA(s)

add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
  • 1
    I think you mean `%Y %b`, but regardless a year and a month are not a date. Either paste on a day, or look at `yearmon` objects in **zoo** or **xts**, I can't remember which. – joran Sep 01 '16 at 15:30
  • 1
    You could do: `as.Date(paste0(df$date," 1"),"%Y %b %d")`, where df is your data frame – R. Schifini Sep 01 '16 at 15:39
  • @R.Schifini Fantastic this is a perfect answer. Which it was not mark duplicate because it was not the same question. Can you put this as an answer i want to give credit for this answer. Someone could benefit in future. – add-semi-colons Sep 01 '16 at 15:41
  • 2
    @Null-Hypothesis The second answer at the duplicate shows the "paste a day on and then use `as.Date`" method that you praise so highly. The only difference is that you need `%b` not `%m`. – joran Sep 01 '16 at 15:47
  • 2
    Yes, pasting a specific day was among the answers of the question. Upvote that answer if you found this useful. – R. Schifini Sep 01 '16 at 15:52

0 Answers0