I am trying to use rpy2 to let me use some r functionality in python. Here is a simple regression I want to do. I create a data frame, convert it to R data frame and then try using R's lm. But the R data frame cannot be found (see below). Where should I look to troubleshoot?
FYI I am using python 2.7.3, rpy2-2.3.2, pandas version '0.10.1' and 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