【发布时间】:2017-02-12 19:08:34
【问题描述】:
我正在使用数据框来创建 xts。 xts 被创建,但所有值(xts 中的索引除外)都在引号内。这导致我无法使用数据,因为 sum 等许多函数不起作用。
任何想法如何在没有引号的情况下生成 xts?
这是我的代码 [由于数据帧/xts 名称不一致的评论而更新]:
# creates a dataframe with dates and currency
mydf3 <- data.frame(date = c("2013-12-10", "2015-04-01",
"2016-01-03"), plus = c(4, 3, 2), minus = c(-1, -2, -4))
# transforms the column date to date-format
mydf3 = transform(mydf3,date=as.Date(as.character(date),format='%Y-%m-%d'))
# creates the xts, based on the dataframe mydf3
myxts3 <- xts(mydf3, order.by = mydf3$date)
# removes the column date, since date is now stored as index in the xts
myxts3$date <- NULL
【问题讨论】:
-
这对您有帮助吗?
t = transform(mydf3,date2=as.Date(mydf3$date,format='%Y-%m-%d')); myxts3 <- xts(t, order.by = t$date2) -
你的变量名不一致。试试
myxts3<-xts(t[,-1],order.by = t$date)