使用 R 将数据帧转换为时间序列 [英] Converting data frame into Time Series using R
本文介绍了使用 R 将数据帧转换为时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个格式的时间序列数据
I have a time series data of the format
Time Ask Bid Trade Ask_Size Bid_Size Trade_Size
2016-11-01 09:00:12 NA 901 NA NA 100 NA
2016-11-01 09:00:21 NA NA 950 NA NA 5
2016-11-01 09:00:21 NA 950 NA NA 5 NA
2016-11-01 09:00:21 905 NA NA 10 NA NA
2016-11-01 09:00:24 NA 921 NA NA 500 NA
2016-11-01 09:00:28 NA 879 NA NA 2 NA
dataframe的结构是
The structure of the dataframe is
str(df)
'data.frame': 35797 obs. of 7 variables:
$ Time : POSIXct, format: "2016-11-01 09:00:12" "2016-11-01 09:00:21" ...
$ Ask : num NA NA NA 905 NA NA 1040 NA NA 905 ...
$ Bid : num 901 NA 950 NA 921 879 NA NA 950 NA ...
$ Trade : num NA 950 NA NA NA NA NA 950 NA NA ...
$ Ask_Size : num NA NA NA 10 NA NA 6 NA NA 10 ...
$ Bid_Size : num 100 NA 5 NA 500 2 NA NA 5 NA ...
$ Trade_Size: num NA 5 NA NA NA NA NA 5 NA NA ...
我正在尝试使用代码将其转换为时间序列
I am trying to convert it to Time Series using the code
library(zoo)
library(xts)
library(lubridate)
df_ts <- xts(x = df, order.by = df$Time)
但我得到奇怪的输出
Time Ask Bid Trade Ask_Size Bid_Size Trade_Size
2016-11-01 01:00:03 "2016-11-01 01:00:03" NA "938.10" NA NA " 203" NA
2016-11-01 01:00:04 "2016-11-01 01:00:04" NA "937.20" NA NA " 100" NA
2016-11-01 01:00:04 "2016-11-01 01:00:04" " 938.00" NA NA " 28" NA NA
2016-11-01 01:00:04 "2016-11-01 01:00:04" NA "938.10" NA NA " 203" NA
2016-11-01 01:00:04 "2016-11-01 01:00:04" " 939.00" NA NA " 11" NA NA
2016-11-01 01:00:05 "2016-11-01 01:00:05" NA "938.15" NA NA " 19" NA
时间"列中的时间出现了两次,并且开始时间是从下午 1:00 开始.时间顺序与原始数据格式不符.(原始数据帧的开始时间是从上午 9:00 开始).请帮忙.
The time in the column "Time" is appearing twice and also the starting time is from 1:00 pm. The order of the time is not as per the original dataformat. (The starting time of the original dataframe is from 9:00 am). Please help.
推荐答案
试试这个:
df_ts <- as.xts(x = df[, -1], order.by = df$Time)
不用说,这会跳过第一列.
Needless to say, this skips the first column.
这篇关于使用 R 将数据帧转换为时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文