我在csv文件中有一些价格数据,我想让日期列为日期
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)
结果如下:
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"
你可以看到我在做数据$ Date&lt; -as.Date(as.character(data $ Date))之后,日期不正确。你知道我怎么能得到正确的日期格式吗?
谢谢。
答案 0 :(得分:1)
嗨,有几种方法可以得到你想要的东西我会指出几个例子:
data$Date <- as.POSIXct(strptime(paste(data$Date), format="%d/%m/%Y"))
或使用包lubridate的非常好的替代方案
library(lubridate)
data$Date<-mdy(data$Date)
干杯