R xts赋值更改列类

R xts赋值更改列类,r,xts,R,Xts,我有一个data.frame,定义如下: earlyCloses <- read.table('EarlyCloses.txt', header=T, colClasses= c(rep("character", 3))) earlyCloses StartDate EndDate EarlyClose 1 2012-12-24 2012-12-24 13:00 prices <- read.table('sample.txt', header=T, col

我有一个data.frame,定义如下:

earlyCloses <- read.table('EarlyCloses.txt', header=T, colClasses= c(rep("character", 3)))
earlyCloses

   StartDate    EndDate EarlyClose
1 2012-12-24 2012-12-24      13:00
prices <- read.table('sample.txt', header=T, colClasses=c("character", "numeric"))
pricesXts = xts(prices$Close, as.POSIXct(prices$Date, tz='America/New_York'))
colnames(pricesXts) = c("Close")
pricesXts$CloseTime = NA
pricesXts

              Close CloseTime
2012-12-21 13190.84        NA
2012-12-24 13139.08        NA
2012-12-26 13114.59        NA
2012-12-27 13096.31        NA
2012-12-28 12938.11        NA

为什么xts对象中Close列的类从数字变为字符?这是因为xts对象在内部表示为矩阵吗?有没有办法避免这种转换?

xts
内部编码为矩阵(性能更好)。由于您只想存储早期关闭,因此可以将其转换为数字,例如:

strptime(earlyCloses$EarlyClose,'%H:%M')$hour
然后

for(1中的i:nrow(earlyCloses))
价格[paste(earlyCloses[i,“StartDate”],
earlyCloses[i,“EndDate”],

sep='/',2]查看函数
xts
,似乎是这样(
矩阵
)。
strptime(earlyCloses$EarlyClose,'%H:%M')$hour
for (i in 1:nrow(earlyCloses))
   pricesXts[paste(earlyCloses[i,"StartDate"], 
                   earlyCloses[i,"EndDate"], 
                   sep='/'), 2] <- strptime(earlyCloses$EarlyClose,'%H:%M')$hour


           Close CloseTime
2012-12-21 13191        NA
2012-12-24 13139        13
2012-12-26 13115        NA
2012-12-27 13096        NA
2012-12-28 12938        NA