0

I have a database which looks like this:

structure(list(State = c("Alaska", "Alaska", "Alaska", "Alaska", 
"Alaska"), month = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), Abbreviation = c("AK", "AK", "AK", "AK", "AK"), date = c("1/31/2011", 
"10/31/2011", "11/30/2011", "12/31/2010", "4/30/2005"), year = c("2011", 
"2011", "2011", "2010", "2005")), row.names = c(NA, 5L), class = "data.frame")

Which has a column with dates called date.

All I want to do is extract the day and the month into separate columns.

I however always get the message that the date is ambiguous, even though there is obviously no 30th or 31st month.

Somehow however, nothing seems to work. I tried:

ar203$Month_Yr <- format(as.Date(ar203$date), "%Y-%m")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

ar203$month <- strftime(ar203$date, "%m")
Error in as.POSIXlt.character(x, tz = tz) : 
  character string is not in a standard unambiguous format

ar203$date <- as.POSIXct(ar203$date)
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

What is going on and how do I fix this?

Tom
  • 2,173
  • 1
  • 17
  • 44

1 Answers1

1

Your date format is %m/%d/%Y so you have to state this in as.Date.

ar203$Month_Yr <- format(as.Date(ar203$date, "%m/%d/%Y"), "%Y-%m")
GKi
  • 37,245
  • 2
  • 26
  • 48