0

I have a date frame with dates.

customer <-c("57","49687","4564","654654")
date     <- c("11-2016","01-2017","02-2017","12-2016")
df       <- data.frame(customer,date)

However, if I want to use as.POSIXct() to change the format, it exhibits a column of NAs but does not show any warning messages.

df$date <- as.POSIXct(as.character(df$date),format = "%m %Y")

How can I use properly as.POSIXct() in this setting?

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
J.dude
  • 3
  • 3
  • 3
    Your dates are missing the days. So `as.POSIXct` is not able to convert them (and btw the format should have been `"%m-%Y"`). – nicola Aug 31 '18 at 12:11
  • POSIXct has a date+time structure. For just the date, you can use `as.Date` – Rohit Aug 31 '18 at 12:28
  • This might be helpful https://stackoverflow.com/questions/6242955/converting-year-and-month-yyyy-mm-format-to-a-date – Ronak Shah Aug 31 '18 at 12:35

1 Answers1

0

As @nicola stated you are missing the day. If you want to assume the day as the first of the month, you can paste in the day as the first using:

df$date <- as.POSIXct(paste(df$date, "01", sep="-"), format="%m-%Y-%d")
Kerry Jackson
  • 1,821
  • 12
  • 20