0

I have a csv file (df) with dates as " Mar-97, Apr-97..." . After importing to r with read.csv and stringAsFactors = F, the class(dates) is character.

I have tried : df$dates <- as.Date(df$Dates , format = "%d-%b-%y") & as.Date(df$Dates , format = "%b-%y"). class is converted to Date but it shows NA values?

arunv
  • 13
  • 5

1 Answers1

0

you can try lubridate library:

 library(lubridate)
> parse_date_time("Mar-97", "m y")
[1] "1997-03-01 UTC"

and you can vectorize

df=c("Mar 17","Apr 17")
> parse_date_time(df, "m y")
[1] "2017-03-01 UTC" "2017-04-01 UTC"
Antonios
  • 1,919
  • 1
  • 11
  • 18
  • . Thanks Antonio but the class(df$dates) [1] "POSIXct" "POSIXt". Doesnt work – arunv Mar 03 '18 at 20:51
  • after parse_date_time the class(df$dates) was [1] "POSIXct" "POSIXt". Then I used as.Date(df$dates, format ="%Y-%m-%d" and it worked ! Thanks – arunv Mar 03 '18 at 20:59