数字日期到字符串日期

时间:2013-03-11 02:00:14

标签: r

我将时间序列日期加载到R中作为数据框。但是,当我查看数据时,时间序列日期不会显示为字符串,尽管日期列的类被识别为日期类。在不使用zoo的情况下,如何让数据集以字符串格式显示日期。这实际上是出于视觉目的,能够查看数据是否正确。

Date        Open    High    Low Close   Volume  Adj Close
3/8/2013    834.5   834.92  825.05  831.52  2911900 831.52
3/7/2013    834.06  836.62  829.58  832.6   2052700 832.6
3/6/2013    841.03  844 828.81  831.38  2873000 831.38
3/5/2013    828.93  840.15  828.9   838.6   4044100 838.6
3/4/2013    805.3   822.84  805 821.5   2775600 821.5

在R中显示为:

Date    Open    High    Low Close   Volume  Adj.Close
15772   834.50  834.92  825.05  831.52  2911900 831.52
15771   834.06  836.62  829.58  832.60  2052700 832.60
15770   841.03  844.00  828.81  831.38  2873000 831.38
15769   828.93  840.15  828.90  838.60  4044100 838.60
15768   805.30  822.84  805.00  821.50  2775600 821.50
15765   797.80  807.14  796.15  806.19  2175400 806.19

我正在使用命令

data=read.csv("file location",header=T,colClasses=c("Date","numeric","numeric","numeric","numeric","numeric","numeric"))

这是输出输出:

dput(head(data))
structure(list(Date = structure(c(15772, 15771, 15770, 15769, 
15768, 15765), class = "Date"), Open = c(834.5, 834.06, 841.03, 
828.93, 805.3, 797.8), High = c(834.92, 836.62, 844, 840.15, 
822.84, 807.14), Low = c(825.05, 829.58, 828.81, 828.9, 805, 
796.15), Close = c(831.52, 832.6, 831.38, 838.6, 821.5, 806.19
), Volume = c(2911900, 2052700, 2873000, 4044100, 2775600, 2175400
), Adj.Close = c(831.52, 832.6, 831.38, 838.6, 821.5, 806.19)), .Names = c("Date", 
"Open", "High", "Low", "Close", "Volume", "Adj.Close"), row.names = c(NA, 
6L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

您需要使用as.Date,您必须知道数据的起源日期。例如,假设您知道日期数据来自1970-01-01,您可以执行此操作进行转换:

dates <- c(1314, 1315, 1316)
as.Date(dates, format="%Y-%m-%d", origin="1970-01-01")

[1] "1973-08-07" "1973-08-08" "1973-08-09"

<强>更新

根据里卡多的评论,您的原始数据似乎是1997-05-28

as.Date(dates, format="%Y-%m-%d", origin="1997-05-28")

[1] "2001-01-01" "2001-01-02" "2001-01-03"

答案 1 :(得分:0)

您可以将日期列保留为字符,然后将其转换为日期。

或者如read.table的帮助所示:

  

否则需要一个as方法(来自包方法)   从“字符”转换为指定的正式类。

请参阅this question如何应用此功能。

setClass("myDate")
setAs("character","myDate", function(from) as.Date(from, format="%d/%m/%Y"))

df <- read.table(header = TRUE, colClasses=c("myDate","numeric","numeric","numeric","numeric","numeric","numeric"),
text = 'Date  Open  High  Low Close VolumeAdj Close
3/8/2013    834.5   834.92  825.05  831.52  2911900 831.52
3/7/2013    834.06  836.62  829.58  832.6   2052700 832.6
3/6/2013    841.03  844 828.81  831.38  2873000 831.38
3/5/2013    828.93  840.15  828.9   838.6   4044100 838.6
3/4/2013    805.3   822.84  805 821.5   2775600 821.5')

str(df)
'data.frame':   5 obs. of  7 variables:
 $ Date     : Date, format: "2013-08-03" "2013-07-03" "2013-06-03" ...
 $ Open     : num  834 834 841 829 805
 $ High     : num  835 837 844 840 823
 $ Low      : num  825 830 829 829 805
 $ Close    : num  832 833 831 839 822
 $ VolumeAdj: num  2911900 2052700 2873000 4044100 2775600
 $ Close.1  : num  832 833 831 839 822
相关问题