简体   繁体   English

RPy2的奇怪问题

[英]Strange Problem with RPy2

After installing RPy2 from 从中安装RPy2后

http://rpy.sourceforge.net/rpy2.html http://rpy.sourceforge.net/rpy2.html

I'm trying to use it in Python 2.6 IDLE but I'm getting this error: 我正在尝试在Python 2.6 IDLE中使用它,但我收到此错误:

>>> import rpy2.robjects as robjects
>>> robjects.r['pi']

<RVector - Python:0x0121D8F0 / R:0x022A1760>

What I'm doing wrong? 我做错了什么?

Have you tried looking at the vector that's returned? 您是否尝试过查看返回的向量?

 >>> pi = robjects.r['pi']
 >>> pi[0]
 3.14159265358979

To expand on Shane's answer. 扩展Shane的答案。 rpy2 uses the following Python objects to represent the basic R types: rpy2使用以下Python对象来表示基本的R类型:

  • RVector: R scalars and vectors, R Lists are represented as RVectors with names, see below RVector:R标量和向量,R列表表示为具有名称的RVectors,见下文
  • RArray: an R matrix, essentially the RVector with a dimension RArray:一个R矩阵,基本上是具有维度的RVector
  • RDataFrame: an R data.frame RDataFrame:R data.frame

To coerce back to basic Python types look here . 要强制回到基本的Python类型, 请看这里

As an example, I use this to convert an R List to a python dict: 举个例子,我使用它将R List转换为python dict:

rList = ro.r('''list(name1=1,name2=c(1,2,3))''')
pyDict = {}
for name,value in zip([i for i in rList.getnames()],[i for i in rList]):
    if len(value) == 1: pyDict[name] = value[0]
    else: pyDict[name] = [i for i in value]

In the Python interactive interpreter if an expression returns a value then that value is automatically printed. 在Python交互式解释器中,如果表达式返回一个值,则会自动打印该值。 For example if you create a dictionary and extract a value from it the value is automatically printed, but if this was in an executing script this would not be the case. 例如,如果您创建一个字典并从中提取值,则会自动打印该值,但如果这是在执行脚本中,则情况并非如此。 Look at the following simple example this is not an error but simply python printing the result of the expression: 看看下面这个简单的例子,这不是错误,只是python打印表达式的结果:

>>> mymap = {"a":23}
>>> mymap["a"]
23

The same code in a python script would produce no output at all. python脚本中的相同代码根本不会产生任何输出。

In your code you are accessing a map like structure with the code: 在您的代码中,您使用代码访问类似结构的地图:

>>> robjects.r['pi']

This is returning some R2Py object for which the default string representation is: <RVector - Python:0x0121D8F0 / R:0x022A1760> 这将返回一些R2Py对象,其默认字符串表示形式为: <RVector - Python:0x0121D8F0 / R:0x022A1760>

If you changed the code to something like: 如果您将代码更改为:

pi = robjects.r['pi']

you would see no output but the result of the call (a vector) will be assigned to the variable pi and be available for you to use. 您将看不到输出,但调用的结果(向量)将被分配给变量pi并可供您使用。

Looking at the R2Py documentation It seems many of the objects are by default printed as a type in <> brackets and some memory address information. 查看R2Py文档默认情况下,许多对象默认打印为<>括号中的类型和一些内存地址信息。

This is not an error, it's simply the 'repr' of the returned robject: 这不是错误,它只是返回的robject的'repr':

>>> r['pi']
<RVector - Python:0x2c14bd8 / R:0x3719538>
>>> repr(r['pi'])
'<RVector - Python:0x4b77908 / R:0x3719538>'
>>> str(r['pi'])
'[1] 3.141593'
>>> print r['pi']
[1] 3.141593

You can get the value of 'pi' accessing it by index 您可以通过索引获取“pi”访问它的值

>>> r['pi'][0]
3.1415926535897931

To access element of named lists (the ' object$attribute ' R syntax) I use 要访问命名列表的元素(' object $ attribute'R语法),我使用

>>> l = r.list(a=r.c(1,2,3), b=r.c(4,5,6))
>>> print l
$a
[1] 1 2 3

$b
[1] 4 5 6

>>> print dict(zip(l.names, l))['a']
[1] 1 2 3

but I think there must be a better solution... 但我认为必须有更好的解决方案......

I found this as the only sensible, short discussion of how to go back and forth from R objects and python. 我发现这是关于如何从R对象和python来回走动的唯一合理,简短的讨论。 naufraghi's solution prompted the following approach to converting a data.frame, which retains the nicer slicing capabilities of the dataframe: naufraghi的解决方案促使了以下方法转换data.frame,它保留了数据帧的更好的切片功能:

In [69]: import numpy as np

In [70]: import rpy2.robjects as ro

In [71]: df = ro.r['data.frame'](a=r.c(1,2,3), b=r.c(4.0,5.0,6.3))

In [72]: df
Out[72]: <RDataFrame - Python:0x5492200 / R:0x4d00a28>

In [73]: print(df)
  a   b
1 1 4.0
2 2 5.0
3 3 6.3

In [74]: recdf = np.rec.fromarrays(df, names=tuple(df.names))

In [75]: recdf
Out[75]: 
rec.array([(1, 4.0), (2, 5.0), (3, 6.2999999999999998)], 
      dtype=[('a', '<i4'), ('b', '<f8')])

Seems a bit off-topic at this point, but I'm not sure what the appropriate procedure would be to capture this question & answer of mine! 在这一点上看起来有点偏离主题,但我不确定采取这个问题和答案的适当程序是什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM