转换为xts无效,因为数据的类型为“字符",并且无法转换为“数字" [英] Conversion to xts does not work because data is of type 'character' and cannot be converted to 'numeric'

查看:53
本文介绍了转换为xts无效,因为数据的类型为“字符",并且无法转换为“数字"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集

data
     date PX_LAST.USGG10YR Index PX_LAST.GSWISS10 Index
1  2012-12-31                 1.7574                  0.526
2  2013-01-31                 1.9849                  0.789
3  2013-02-28                 1.8756                  0.698
4  2013-03-29                 1.8486                  0.716
5  2013-04-30                 1.6717                  0.570
6  2013-05-31                 2.1282                  0.722
7  2013-06-28                 2.4857                  1.027
8  2013-07-31                 2.5762                  1.023
9  2013-08-30                 2.7839                  1.069
10 2013-09-30                 2.6100                  1.021

日期列的类别为

> class(data[,1])
[1] "POSIXct" "POSIXt"

其他两列的类是

class(data [,2])[1]数字"类(数据[,3])[1]数字"

class(data[,2]) [1] "numeric" class(data[,3]) [1] "numeric"

我现在将其转换为

df2=xts(data,order.by=data$date)

结果是

structure(c("2012-12-31", "2013-01-31", "2013-02-28", "2013-03-29", 
"2013-04-30", "2013-05-31", "2013-06-28", "2013-07-31", "2013-08-30", 
"2013-09-30", "1.7574", "1.9849", "1.8756", "1.8486", "1.6717", 
"2.1282", "2.4857", "2.5762", "2.7839", "2.61", "0.526", "0.789", 
"0.698", "0.716", "0.57", "0.722", "1.027", "1.023", "1.069", 
"1.021"), .Dim = c(10L, 3L), .Dimnames = list(NULL, c("date", 
"PX_LAST.USGG10YR Index", "PX_LAST.GSWISS10 Index")), index = structure(c(1356912000, 
1359590400, 1362009600, 1364515200, 1367280000, 1369958400, 1372377600, 
1375228800, 1377820800, 1380499200), tzone = "UTC", tclass = c("POSIXct", 
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), .indexTZ = "UTC", tzone = "UTC", class = c("xts", 
"zoo"))

看结果,我得到

> typeof(df2)
[1] "character"

很显然,我不能将其用于计算(例如Return.calculate).当我尝试使用as.numeric(df2 [,2])转换第2列和第3列时,它仍然保留为'character'类型.有人可以帮我找到一个将其转换为适当的xts的代码(即第2列和第3列的类型为"double"),即df2的结构类似于本示例

Clearly, I cannot use this for calculations (such as Return.calculate). When I try to convert columns 2 and 3 with as.numeric(df2[,2]), it remains as type 'character'. Can somebody help me to find a code which does convert this to a proper xts (i.e. columns 2 and 3 are of type 'double'), i.e. the structure of df2 is like in this example

library(quantmod)
getSymbols('AAA',src='FRED')
class(AAA)
typeof(AAA)
> class(AAA)
[1] "xts" "zoo"
> typeof(AAA)
[1] "double

问候安德烈亚斯(Andreas)

Regards Andreas

推荐答案

您在xts数据中包含了 date 列,因此R使用不会不会丢失任何信息(字符).数据中没有 date 列作为xts索引,因此在调用 xts()时只需忽略该列.

You included the date column in the xts data, so R converted your data.frame into a matrix using the type that wouldn't lose any information (character). There's no need to have the date column as the xts index and in the data, so just omit that column when you call xts().

df2 <- xts(data[,-1], order.by=data$date)

这篇关于转换为xts无效,因为数据的类型为“字符",并且无法转换为“数字"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆