6

我有日期、时间、值格式的数据。这是一个示例:

04/01/2010,07:10,17159
04/01/2010,07:20,4877
04/01/2010,07:30,6078
04/01/2010,07:40,3105
04/01/2010,07:50,4073
04/01/2010,08:00,6986
04/01/2010,08:10,7906
04/01/2010,08:20,7681
04/01/2010,08:30,5665
04/01/2010,08:40,6631
04/01/2010,08:50,4633
04/01/2010,09:00,6346
04/01/2010,09:10,6444
04/01/2010,09:20,6324
04/01/2010,09:30,11696
04/01/2010,09:40,7667
04/01/2010,09:50,6375
04/01/2010,10:00,5934
04/01/2010,10:10,12626
04/01/2010,10:20,11674
04/01/2010,10:30,4660
04/01/2010,10:40,3831
04/01/2010,10:50,7089
04/01/2010,11:00,4548
04/01/2010,11:10,2590
04/01/2010,11:20,3334
04/01/2010,11:30,5171

我想将其转换为保持相同格式的时间序列值。即我也需要能够存储日期和时间组件。这是因为我想“去季节化”数据。

我努力了

z <- read.csv("fileName", header=TRUE,sep=",")

但不知道从这里做什么。谁能告诉我如何正确加载到时间序列对象中?还是有其他方法可以做到这一点?

提前致谢

4

2 回答 2

9

您可以使用该zoo软件包。下面的代码被编写为可重现的,但在实际实践text="Lines"中将替换为file="fileName". 同样如问题所示,日期字段不明确,如果不是日/月/年,您可能需要调整百分比代码。

library(zoo)

Lines <- "Date,Time,Value
04/01/2010,07:10,17159
04/01/2010,07:20,4877
04/01/2010,07:30,6078
04/01/2010,07:40,3105
"

z <- read.zoo(text = Lines, sep = ",", header = TRUE, 
       index = 1:2, tz = "", format = "%d/%m/%Y %H:%M")

这使:

> z
2010-01-04 07:10:00 2010-01-04 07:20:00 2010-01-04 07:30:00 2010-01-04 07:40:00 
              17159                4877                6078                3105 
于 2013-09-16T13:06:30.670 回答
0

除了作为答案提到的内容外,您还可以查看此链接(http://eclr.humanities.manchester.ac.uk/index.php/R_TSplots),该链接讨论了在这种情况下“xts”的使用。我希望它有所帮助。

于 2019-06-12T05:31:50.347 回答