本文介绍了R 将日期附加到时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 excel 电子表格,其中包含第一行日期和随后的列,这些列指的是这些日期不同证券的价格.

I have a spreadsheet in excel which consists of first row of dates and then subsequent columns that refer to prices of different securities on those dates.

我将excel文件保存为csv,然后使用

I saved the excel file as a csv and then imported to excel using

prices=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Option prices.csv",header = TRUE, sep = ",")

这将创建正确的时间序列数据

This creates the correct time series data

x<-ts(prices[,2])

但没有附上日期.

然而,日期指的是工作日.因此,尽管它们通常代表周一至周五,但由于假期等原因,情况并非总是如此.

However the dates refer to working days. So although in general they represent Monday-Friday this is not always the case because of holidays etc.

然后如何创建一个时间序列,其中从 csv 文件的第一列读取日期?我在 R 中找不到完成此操作的示例

How then can I create a time series where the dates are read in from the first column of the csv file? I can not find an example in R where this is done

推荐答案

由于你没有提供任何数据,这里是一个虚构的 data.frame:

As you didn't give any data, here is a made-up data.frame:

R> DF <- data.frame(date="2011-05-15", time=c("08:25:00", "08:45:00", 
+                   "09:05:11"), val=rnorm(3, 100, 5))
R> DF
        date     time     val
1 2011-05-15 08:25:00 99.5926
2 2011-05-15 08:45:00 95.8724
3 2011-05-15 09:05:11 96.6436
R> DF <- within(DF, posix <- as.POSIXct(paste(date, time)))
R> DF
        date     time     val               posix
1 2011-05-15 08:25:00 99.5926 2011-05-15 08:25:00
2 2011-05-15 08:45:00 95.8724 2011-05-15 08:45:00
3 2011-05-15 09:05:11 96.6436 2011-05-15 09:05:11
R> 

我使用了within(),您可以使用其他方式来分配新列.关键是 paste() 允许您组合列,您可以根据需要使用其他 R 函数来修改数据.

I used within(), you can use other means to in order to assign new columns. The key is that paste() allows you to combine columns, and you could use other R functions to modify the data as needed.

以合适的类型(如POSIXct)解析日期和时间的主要优点是其他函数可以使用它.这是动物园:

The key advantage of having dates and times parsed in a suitable type (like POSIXct) is that other functions can then use it. Here is zoo:

R> z <- with(DF, zoo(val, order.by=posix))
R> summary(z)
     Index                              z       
 Min.   :2011-05-15 08:25:00.00   Min.   :95.9  
 1st Qu.:2011-05-15 08:35:00.00   1st Qu.:96.3  
 Median :2011-05-15 08:45:00.00   Median :96.6  
 Mean   :2011-05-15 08:45:03.67   Mean   :97.4  
 3rd Qu.:2011-05-15 08:55:05.50   3rd Qu.:98.1  
 Max.   :2011-05-15 09:05:11.00   Max.   :99.6  
R> 

这篇关于R 将日期附加到时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 02:11