Can';t从<;fctr>;至<;日期>;充分
这有点奇怪。我以前曾将数据从.csv转换为.xts,但由于某些原因,这次不能 这是我的数据集(Can';t从<;fctr>;至<;日期>;充分,r,date,xts,R,Date,Xts,这有点奇怪。我以前曾将数据从.csv转换为.xts,但由于某些原因,这次不能 这是我的数据集(dput()占实际数据集的一半,因为完整的数据集超出了字符限制。是的,问题仍然存在): 因此,我通常要做的是编写以下代码: library(fBasics) pw_index <- read.csv("~/data/index.csv", header=T) # Set time in date format index$time <- as.Date(
dput()
占实际数据集的一半,因为完整的数据集超出了字符限制。是的,问题仍然存在):
因此,我通常要做的是编写以下代码:
library(fBasics)
pw_index <- read.csv("~/data/index.csv",
header=T)
# Set time in date format
index$time <- as.Date(index$time, format="%d/%m/%y")
index <- index[order(index$time), ]
# Save the date in a separate identifier as character
dates = as.character(index$time)
index <- index[order(dates), ]
# Convert the data frame to an .xts object:
index_xts <- as.xts(index$index, order.by=index$time)
head(index_xts)
然而,我在代码之后得到的是一个完全混乱的数据集(最后一次观察应该是2016年…):
谁知道发生了什么事?您的代码有些复杂,我不完全确定您想做什么。要将
data.frame
中的数据转换为xts
对象,可以执行以下操作:
library(xts);
xts <- xts(x = df$index, order.by = as.POSIXct(df$time, format = "%d/%m/%Y"));
tail(xts);
# [,1]
#2016-04-19 1.624924
#2016-04-20 1.555608
#2016-04-21 1.474775
#2016-04-22 1.438227
#2016-04-23 1.664659
#2016-04-24 1.499378
库(xts);
xts
time index
<fctr> <dbl>
1 17/07/2014 11.54043
2 18/07/2014 14.27814
3 19/07/2014 11.55830
4 20/07/2014 12.37828
5 21/07/2014 12.54057
6 22/07/2014 12.10189
[,1]
2020-01-01 2.708465
2020-01-01 2.268203
2020-01-02 2.567833
2020-01-02 1.695811
2020-01-03 2.783192
2020-01-03 1.714067
library(xts);
xts <- xts(x = df$index, order.by = as.POSIXct(df$time, format = "%d/%m/%Y"));
tail(xts);
# [,1]
#2016-04-19 1.624924
#2016-04-20 1.555608
#2016-04-21 1.474775
#2016-04-22 1.438227
#2016-04-23 1.664659
#2016-04-24 1.499378