Reputation: 9018
I have some price data in a csv file and I want to make the date column a date
data<- read.csv("mypath//A.csv", 1)
head(data)
class(data$Date)
data$Date<-as.Date(as.character(data$Date))
head(data)
class(reuters$Date)
here are the results:
data<- read.csv("path//A.csv", 1)
> head(data)
Date O H L C V
1 10/1/2014 40.65270 40.70990 40.17359 40.18789 2481276
2 10/2/2014 40.13783 40.21649 39.53716 39.92331 1708052
3 10/3/2014 40.20934 40.76711 40.15929 40.69560 1577280
4 10/6/2014 40.80286 40.87437 40.35236 40.45247 1157370
5 10/7/2014 40.22364 40.28085 39.32979 39.32979 1515558
6 10/8/2014 39.37269 40.41672 39.18677 40.37381 2124682
> class(data$Date)
[1] "factor"
> data$Date<-as.Date(as.character(data$Date))
> head(data)
Date O H L C V
1 0010-01-20 40.65270 40.70990 40.17359 40.18789 2481276
2 0010-02-20 40.13783 40.21649 39.53716 39.92331 1708052
3 0010-03-20 40.20934 40.76711 40.15929 40.69560 1577280
4 0010-06-20 40.80286 40.87437 40.35236 40.45247 1157370
5 0010-07-20 40.22364 40.28085 39.32979 39.32979 1515558
6 0010-08-20 39.37269 40.41672 39.18677 40.37381 2124682
> class(data$Date)
[1] "Date"
You can see that after i do data$Date<-as.Date(as.character(data$Date)) the date is not correct. Do you know how I can get the correct date format?
Thank you.
Upvotes: 0
Views: 97
Reputation: 197
Hi there are a few ways of getting what you want I'll point out a couple of examples:
data$Date <- as.POSIXct(strptime(paste(data$Date), format="%d/%m/%Y"))
or a very good alternative using the package lubridate
library(lubridate)
data$Date<-mdy(data$Date)
Cheers
Upvotes: 1