我对这个看似简单的操作有困难,我认为此操作先前已经解决,但我找不到任何例子。
在R环境中,@ dat是DataFrame类型,但rpy2(2.2)返回Matrix。有没有一种方法可以将m转换为DataFrame或让robjects.r(查询)返回一个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)
答案 0 :(得分:2)
您可以尝试显式转换为yahooImport函数中的data.frame。
以下对我有用:
>>> 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返回一个timeSeries对象,该对象在内部表示为矩阵,这是导致rpy2中的返回值为矩阵的原因。