-1

I have a csv file with 2 coloumns-price and date.I am trying to do time series forecasting and for that I need to convert the date coloumn to date-time class. Presently the class of Date coloumn is NULL.I tried to convert it to character format,but its not working.Also,is there a way that I can do this directly in csv file in excel?

wti.daily <- read.csv(file="wti.csv",stringsAsFactors = FALSE)
> wti.daily$date <- as.Date(as.character(wti.daily$date))
Error in `$<-.data.frame`(`*tmp*`, date, value = numeric(0)) : 
replacement has 0 rows, data has 1598
> class(wti.daily$date)
[1] "NULL"
> wti.daily$Date = as.Date(wti.daily$date)
Error in as.Date.default(wti.daily$date) : 
do not know how to convert 'wti.daily$date' to class “Date”
> wti.daily$date <- as.Date(wti.daily$date,format = "%m/%d/%y")
Error in as.Date.default(wti.daily$date, format = "%m/%d/%y") : 
> head(wti.daily)
Date  Price
1 02-01-2012  98.83
2 03-01-2012 102.96
3 04-01-2012 103.22
Bunty
  • 51
  • 2
  • 3
  • 8

1 Answers1

0

From your sample, the "%m-%d-%Y" format is assumed. Try this

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