rpy2(2.3.10版)-将数据从R包导入python
因此,我正在尝试将一些R包中的数据导入python,以便测试我编写的其他python-rpy2函数.特别是,我在R和pennLC
数据集中使用了SpatialEpi
包.
So I am trying to import some data from an R package into python in order to test some other python-rpy2 functions that I have written. In particular, I am using the SpatialEpi
package in R and the pennLC
dataset.
因此,我能够导入rpy2软件包并正确连接到该软件包.但是,我不确定如何访问包中的数据.
So I was able to import the rpy2 package and connect to the package correctly. However, I am not sure how to access the data in the package.
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
spep = importr("SpatialEpi")
但是,我似乎无法访问SpatialEpi
包中的数据对象pennLC
来测试该功能.等效的R命令为:
However, I can't seem to access the data object pennLC
in the SpatialEpi
package to test the function. The equivalent R command would be:
data(pennLC)
任何建议.
在R中,执行data("foo")
可以在工作空间中创建任意数量的对象.在rpy2
中,事物包含在环境中.这使它更干净.
In R, doing data("foo")
can create an arbitrary number of objects in the workspace. In rpy2
things are contained in an environment. This is making it cleaner.
from rpy2.robjects.packages import importr, data
spep = importr("SpatialEpi")
pennLC_data = data(spep).fetch('pennLC')
pennLC_data
是Environment
(将其视为名称空间).
pennLC_data
is an Environment
(think of it as a namespace).
列出获取的内容:
pennLC_data.keys()
要获取所需的数据对象:
To get the data object wanted:
pennLC_data['pennLC'] # guessing here, it might be a different name