我正在尝试使用 rpy2 让我在 python 中使用一些 r 功能。这是我想做的一个简单的回归。我创建了一个数据框,将其转换为 R 数据框,然后尝试使用 R 的 lm。但是找不到 R 数据框(见下文)。我应该去哪里进行故障排除?
仅供参考,我使用的是 python 2.7.3、rpy2-2.3.2、pandas 版本 '0.10.1' 和 R2.15.3
>>> import rpy2
>>> import pandas as pd
>>> import pandas.rpy.common as com
>>> datframe = pd.DataFrame({'a' : [1, 2, 3], 'b' : [3, 4, 5]})
>>> r_df = com.convert_to_r_dataframe(datframe)
>>> r_df
(DataFrame - Python:0x32547e8 / R:0x345d640)
[IntVector, IntVector]
a: (class 'rpy2.robjects.vectors.IntVector')
(IntVector - Python:0x3254e18 / R:0x345d608)
[ 1, 2, 3]
b: (class 'rpy2.robjects.vectors.IntVector')
(IntVector - Python:0x3254e60 / R:0x345d5d0)
[ 3, 4, 5]
>>> print type(r_df)
(class 'rpy2.robjects.vectors.DataFrame')
>>> from rpy2.robjects import r
>>> r('lmout <- lm(r_df$a ~ r_df$b)')
Error in eval(expr, envir, enclos) : object 'r_df' not found
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
r('lmout <- lm(r_df$a ~ r_df$b)')
File "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/__init__.py", line 236, in __call__
res = self.eval(p)
File "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 86, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 35, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
RRuntimeError: Error in eval(expr, envir, enclos) : object 'r_df' not found