1

I have a problem in converting a vector into date one by using as.Date. Data is as below.

> new3<-read.csv("Total Load - Day Ahead _ Actual.csv",stringsAsFactors=F)
> colnames(new3)<- c("Date","Hour","Dayahead","Actual")
> str(new3)
'data.frame':   35044 obs. of  4 variables:
 $ Date    : chr  "01-01-2015" "01-01-2015" "01-01-2015" "01-01-2015" ...
 $ Hour    : chr  "0:00" "0:15" "0:30" "0:45" ...
 $ Dayahead: chr  "42955" "42412" "41901" "41355" ...
 $ Actual  : int  42425 42021 42068 41874 41230 40810 40461 40160 39958 
 ... 

Here, I tried as.Data

new3$Date<-as.Date(new3$Date,"%d/%m/%Y")

The order of d,m,Y is right. But when I do this, it shows me NA in date info as below

> str(new3)
'data.frame':   35044 obs. of  4 variables:
 $ Date    : Date, format: NA NA NA NA ...
 $ Hour    : chr  "0:00" "0:15" "0:30" "0:45" ...
 $ Dayahead: chr  "42955" "42412" "41901" "41355" ...
 $ Actual  : int  42425 42021 42068 41874 41230 40810 40461 40160 39958 
 ...

I don't know what to do to fix it.

Can anyone help me out here? Thank you

junmouse
  • 155
  • 1
  • 9

1 Answers1

3

The step doesn't seem right

new3$Date<-as.Date(new3$Date,"%d/%m/%Y")

You should try using

new3$Date<-as.Date(new3$Date,"%d-%m-%Y")

The separator for date in your date seems to be - and not /

I'll suggest looking into lubridate package as well. It allows you easy ways to convert date from character to date format.

MKR
  • 19,739
  • 4
  • 23
  • 33
  • @junmouse You are welcome. Good to know it worked. You can accept the answer by clicking on tick image left of answer window. – MKR Feb 04 '18 at 15:59