1

I have date in jan-16 format. They are in factor data type. R is recognizing them as character. I want these dates to be arranged in calendar sequence. When I try to plot a graph it gives me in alphabetical order which I don't want. I am getting an error saying

Error in CharToDate(x) : character string is not in a standard unambiguous format.

  • You can't really convert a month-year format into a date class in R without either adding a day value or using an external package. One way would be `Sys.setlocale("LC_TIME", "C") ; as.Date(paste0("01-", "jan-16"), "%d-%b-%y")`. Otherwise, take a look in the dupe. Also, please closely investigate `?strptime`- this should have everything you need to know about dates in r. – David Arenburg Jan 07 '16 at 07:35
  • @DavidArenburg I'm assuming there is a year in there as well, which he did not mention to us. – Tim Biegeleisen Jan 07 '16 at 08:33

1 Answers1

1

You can convert your factor date vector to Date type, and then plot it. Most R plotting packages should be able to order numerically on the date.

dates.raw <- c("Jan 16 2015", "Jan 16 2016")
dates.formatted <- as.Date(dates.raw, format = "%B %d %Y")

> dates.formatted
[1] "2015-01-16" "2016-01-16"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    I might be mistaken, but it doesn't totally address the OP's problem. How about using `dates.raw <- as.factor(c("dec-15", "jan-16"))`, to mimic the format the OP is working with (as far as I can understand), –  Jan 07 '16 at 06:15
  • @Pascal It does work, I just checked. – Tim Biegeleisen Jan 07 '16 at 06:18
  • OP is already trying to convert his factor to a date. Where do you think he got his `CharToDate` error from? Try `as.Date("jan-16")`. The issue here really, is that OP dates dont have a day value. This is just a dupe. – David Arenburg Jan 07 '16 at 07:31