rpy2(2.3.10版)-将数据​​从R包导入python [英] rpy2 (version 2.3.10) - importing data from R package into python

查看:113
本文介绍了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_dataEnvironment(将其视为名称空间).

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

这篇关于rpy2(2.3.10版)-将数据​​从R包导入python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆