Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何组合前两列并将数据转换为xts格式,以便分析时间序列?_R_Time Series_Xts_Zoo - Fatal编程技术网

R 如何组合前两列并将数据转换为xts格式,以便分析时间序列?

R 如何组合前两列并将数据转换为xts格式,以便分析时间序列?,r,time-series,xts,zoo,R,Time Series,Xts,Zoo,正如您所看到的,这是我在R中的数据帧片段。如何组合前两列并将数据转换为xts格式,以便分析时间序列?试试看 Date Timestamp Open High Low Close Volume 1 20131020 22:00:00 1.61730 1.61730 1.61727 1.61727 0.30 2 20131020 22:01:00 1.61722 1.61727 1.61686 1.61686 23.28 3 201310

正如您所看到的,这是我在R中的数据帧片段。如何组合前两列并将数据转换为xts格式,以便分析时间序列?

试试看

Date    Timestamp   Open    High    Low Close   Volume
1   20131020    22:00:00    1.61730 1.61730 1.61727 1.61727 0.30
2   20131020    22:01:00    1.61722 1.61727 1.61686 1.61686 23.28
3   20131020    22:02:00    1.61682 1.61707 1.61670 1.61698 41.77
4   20131020    22:03:00    1.61695 1.61701 1.61695 1.61695 9.41
5   20131020    22:04:00    1.61680 1.61680 1.61680 1.61680 4.62
6   20131020    22:05:00    1.61682 1.61698 1.61682 1.61698 2.13
7   20131020    22:06:00    1.61684 1.61706 1.61684 1.61706 9.64
8   20131020    22:07:00    1.61701 1.61701 1.61683 1.61686 7.01
9   20131020    22:08:00    1.61692 1.61693 1.61686 1.61686 6.00
10  20131020    22:09:00    1.61686 1.61692 1.61670 1.61692 3.28
11  20131020    22:10:00    1.61683 1.61683 1.61681 1.61681 2.00
12  20131020    22:11:00    1.61687 1.61687 1.61662 1.61681 3.03
13  20131020    22:12:00    1.61664 1.61688 1.61664 1.61688 5.78
14  20131020    22:13:00    1.61688 1.61695 1.61688 1.61695 8.93
indx1)
read.zoo
在zoo软件包中,这样做相当简单
index=1:2
告诉它使用第1列和第2列作为索引。
tz=”“
足以让它知道您打算建立一个POSIXct索引。(在zoo的开发版本中,
tz=”“
可以省略,但它仍会解决。)

3)因为如果您只需要计时,那么所有行中的第一列都是相同的:

library(chron)

FUN = function(d, t) as.chron(paste(d, t), format = "%Y%m%d %H:%M:%S")
z <- read.zoo(DF, index = 1:2, FUN = FUN)
as.xts(z)
库(chron)
Z
df <- structure(list(Date = c(20131020L, 20131020L, 20131020L, 20131020L, 
 20131020L, 20131020L, 20131020L, 20131020L, 20131020L, 20131020L, 
20131020L, 20131020L, 20131020L, 20131020L), Timestamp = c("22:00:00", 
"22:01:00", "22:02:00", "22:03:00", "22:04:00", "22:05:00", "22:06:00", 
"22:07:00", "22:08:00", "22:09:00", "22:10:00", "22:11:00", "22:12:00", 
"22:13:00"), Open = c(1.6173, 1.61722, 1.61682, 1.61695, 1.6168, 
1.61682, 1.61684, 1.61701, 1.61692, 1.61686, 1.61683, 1.61687, 
1.61664, 1.61688), High = c(1.6173, 1.61727, 1.61707, 1.61701, 
1.6168, 1.61698, 1.61706, 1.61701, 1.61693, 1.61692, 1.61683, 
1.61687, 1.61688, 1.61695), Low = c(1.61727, 1.61686, 1.6167, 
1.61695, 1.6168, 1.61682, 1.61684, 1.61683, 1.61686, 1.6167, 
1.61681, 1.61662, 1.61664, 1.61688), Close = c(1.61727, 1.61686, 
1.61698, 1.61695, 1.6168, 1.61698, 1.61706, 1.61686, 1.61686, 
1.61692, 1.61681, 1.61681, 1.61688, 1.61695), Volume = c(0.3, 
23.28, 41.77, 9.41, 4.62, 2.13, 9.64, 7.01, 6, 3.28, 2, 3.03, 
5.78, 8.93)), .Names = c("Date", "Timestamp", "Open", "High", 
"Low", "Close", "Volume"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14"))
library(xts)

z <- read.zoo(DF, index = 1:2, tz = "", format = "%Y%m%d %H:%M:%S")
as.xts(z)
library(chron)

FUN = function(d, t) as.chron(paste(d, t), format = "%Y%m%d %H:%M:%S")
z <- read.zoo(DF, index = 1:2, FUN = FUN)
as.xts(z)
library(chron)

z <- read.zoo(DF[-1], FUN = times)
as.xts(z)