Reputation: 13666
I am new to R and I am trying to change date format in the data frame for date columns. My date column is in format Mar 13 2007 01:05:123AM
. Now this date format values are same except day change and time remains same. So I was thinking to change it to format as Mar 13 2007
.
I tried the following code:
df <- read.csv("mydata.csv")
df$collectdate <- format(as.Date(df$collectdate,"%b %d %Y"))
but it gives error saying "character string is not in a standard unambiguous format". What can I try next?
Upvotes: 0
Views: 237
Reputation: 886948
You could try:
date <- "Mar 13 2007 01:05:123AM"
gsub("(.*)(?=\\s\\d{2}:).*", "\\1", date, perl=TRUE)
#[1] "Mar 13 2007"
For the as.Date
, it didn't show any errors.
format(as.Date(date,"%b %d %Y"), "%b %d %Y")
#[1] "Mar 13 2007
Upvotes: 2