convert into date with R

3.4k views Asked by At
> date<-as.character(date)
> head(date)
[1] "14-Jan" "14-Jan" "14-Jan" "14-Jan" "14-Jan" "14-Jan"
> date1<-as.Date(date,format="%y-%b")
> head(date1)
[1] NA NA NA NA NA NA

I wanna convert the date into date format so that i can make a extensible time series data.

1

There are 1 answers

1
Marc in the box On BEST ANSWER

As Scott mentions, you may need to add the day value. Here is another way with paste (assumes the 1st day of the month for all entries):

date <- as.character(c("14-Jan", "14-Jan", "14-Jan", "14-Jan", "14-Jan", "14-Jan"))
date1 <- as.Date(paste0(date, "-1"),format="%y-%b-%d")
head(date1)
#[1] "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01"

you could then go back to displaying a year-month format in the following way:

format(date1, format="%y-%b")
#[1] "14-Jan" "14-Jan" "14-Jan" "14-Jan" "14-Jan" "14-Jan"