4

I am having difficulty with this seemingly simple operation that I'd assume has been solved previously but I cannot find any examples.

In the R environment @data is of type DataFrame but rpy2 (2.2) is returning a Matrix. Is there a convirent way to either convert m to a DataFrame or to have the robjects.r(query) return a DataFrame?

from rpy2.robjects.packages import importr
import rpy2.robjects as robjects

fimport = importr('fImport')

yahooImport = robjects.r('function(x) yahooImport(x)@data')
qqq = yahooImport("QQQ")

print type(qqq)
Marek
  • 49,472
  • 15
  • 99
  • 121
Stephen
  • 125
  • 1
  • 6

1 Answers1

2

You can try explicitly casting to a data.frame in your yahooImport function.

The following worked for me:

>>> yahooImport = robjects.r('function(x) as.data.frame(yahooImport(x)@data)')

>>> print type(qqq)
<class 'rpy2.robjects.vectors.DataFrame'>
>>> qqq
<DataFrame - Python:0x102412680 / R:0x103c2b310>
[Float..., Float..., Float..., Float..., Float..., Float...]
  Open: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x10241a638 / R:0x1048f1c00>
[58.970000, 58.660000, 59.180000, ..., 102.250000, 102.870000, 102.250000]
  High: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x10241acf8 / R:0x104916000>
[59.700000, 59.320000, 59.190000, ..., 102.310000, 103.470000, 102.310000]
  Low: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x10241add0 / R:0x10491c200>
[58.920000, 58.340000, 58.500000, ..., 99.310000, 100.620000, 100.560000]
  Close: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x10241aef0 / R:0x10486f800>
[59.600000, 58.990000, 58.600000, ..., 100.120000, 102.620000, 102.120000]
  Volume: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x10241d050 / R:0x104875a00>
[43540100.000000, 70148800.000000, 57085500.000000, ..., 8743600.000000, 9688600.000000, 5232000.000000]
  Adj.Close: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x10241d170 / R:0x10485a000>
[59.600000, 58.990000, 58.600000, ..., 48.110000, 49.320000, 49.080000]

yahooImport returns a timeSeries object which is represented internally as a matrix, which is what is causing your return values in rpy2 to be a matrix.

evanrsparks
  • 363
  • 3
  • 13