将xts对象从FRED转换为data.table

时间:2017-08-09 14:33:43

标签: r dataframe data.table xts

我有来自FRED的xts对象,并希望将其转换为data.table(或数据帧)对象。相关代码是:

library(data.table)
library(quantmod)
library(Quandl)
library(zoo)
library(knitr)
library(ggplot2)

dataTableTemp <- getSymbols('DJIA', src='FRED')
dataTableTemp <- as.data.table(dataTableTemp)

这是它得到的xts对象的内容:

             DJIA
2007-08-08   13657.86
2007-08-09   13270.68
2007-08-10   13239.54
...          ...          
str。(DJIA),这是下载时给出的名称,

> str(DJIA)
An ‘xts’ object on 2007-08-08/2017-08-08 containing:
  Data: num [1:2610, 1] 13658 13271 13240 13237 13029 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "DJIA"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "FRED"
 $ updated: POSIXct[1:1], format: "2017-08-09 09:41:49"

它持续几千行。当我将它转换为带有第二行代码的data.table时,这就是所有的(以表格式):

             dataTableTemp
1            DJIA

除了

之外,我还尝试过使用ggplot2中的fortify(dataTableTemp)

dataTableTemp <- data.frame(date=index(dataTableTemp), coredata(dataTableTemp)),甚至是tribble()方法,但它们似乎都不起作用。如何将其转换为dataframe / data.table?

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:2)

如果您调整 Traceback (most recent call last): File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/googl e/appengine/runtime/wsgi.py", line 267, in Handle result = handler(dict(self._environ), self._StartResponse) File "/base/data/home/apps/p~svasti-173418/internal- api:20170808t160537.403249868819304873/lib/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/base/data/home/apps/p~svasti-173418/internal- api:20170808t160537.403249868819304873/lib/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/base/data/home/apps/p~svasti-173418/internal- api:20170808t160537.403249868819304873/lib/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() File "/base/data/home/apps/p~svasti-173418/internal-api:20170808t160537.403249868819304873/lib/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/base/data/home/apps/p~svasti-173418/internal-api:20170808t160537.403249868819304873/internal/cron/company_list.py", line 21, in run company_repository.save(row,original_data_source, actual_data_source) File "/base/data/home/apps/p~svasti-173418/internal-api:20170808t160537.403249868819304873/internal/repository/company_repository.py", line 13, in save company.put() File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3458, in _put return self._put_async(**ctx_options).get_result() File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 383, in get_result self.check_success() File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 378, in check_success self.wait() File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 362, in wait if not ev.run1(): File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/ext/ndb/eventloop.py", line 268, in run1 delay = self.run0() File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/ext/ndb/eventloop.py", line 248, in run0 _logging_debug('rpc: %s.%s', rpc.service, rpc.method) File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 453, in service @property DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded. 中的auto.assign参数,则可以获得预期的结果:

getSymbols

答案 1 :(得分:2)

让别人知道这个问题是如何解决的:

getSymbols('DJIA', src='FRED') 
dataTableTemp <- as.data.table(DJIA)