简体   繁体   English

使用rpy2将R ListVector转换为Python列表

[英]Converting a R ListVector to a Python list with rpy2

I have a function that takes the items in a Python list, puts them through a function in R, and outputs them as a R ListVector. 我有一个函数,它接受Python列表中的项目,通过R中的函数将它们输出,并将它们作为R ListVector输出。 The problem is that I can't find in the documentation how to convert from a ListVector into a regular Python object. 问题是我在文档中找不到如何从ListVector转换为常规Python对象。 Here's my code: 这是我的代码:

from rpy2.robjects.packages import importr
from rpy2.robjects import r
forecast = importr("forecast")
parallel = importr("multicore")

data = [[1, 2, 3], [4, 5, 6,], [7, 8, 9]]

tuples = tuple(tuple(x) for x in data)

data_list = []
for i in range(0, len(data)):
    result1 = "k = as.numeric((list%r))" % (tuples[i],)
    data_list.append(result1)

def forecaster(item):
    rcode = item
    r(rcode)
    rcode1 = 'j <- ts(k)'
    r(rcode1)
    rcode2 = 'p <- parallel(forecast(k, 5, level = c(80,95)))'
    r(rcode2)
    rcode3 = 'collect(list(p))'
    return r(rcode3)


z = [forecaster(x) for x in data_list]

Running z gives me output like this: 运行z给我输出如下:

[<ListVector - Python:0x4e5f908 / R:0x4a0fcd8>
[ListVector]
<ListVector - Python:0x4e5f908 / R:0x4a0fcd8>
[ListVector], <ListVector - Python:0x4e5fcf8 / R:0x49f9c48>   

...And so on. ...等等。 Could someone please help me figure out how to convert these ListVectors into something I can actually use in Python? 有人可以帮我弄清楚如何将这些ListVectors转换成我可以在Python中实际使用的东西吗? Thanks. 谢谢。

I change your forecaster function (just last line), using cbind to get an R vector and not a listVector 我改变你的预报功能(只是最后一行),使用cbind获得R矢量而不是listVector

def forecaster(item):
    rcode = item
    r(rcode)
    rcode1 = 'j <- ts(k)'
    r(rcode1)
    rcode2 = 'p <- parallel(forecast(k, 5, level = c(80,95)))'
    r(rcode2)
    return r('c(do.call("cbind",collect(list(p))))')


z = [forecaster(x) for x in data_list]

Now we have in za structure that you can access, eg 现在我们有za结构,你可以访问,例如

z[0]


<ListVector - Python:0x452d908 / R:0x457c770>
[StrVe..., Float..., Float..., ..., RNULL..., Float..., Float...]
  <no name>: <class 'rpy2.robjects.vectors.StrVector'>
  <StrVector - Python:0x452d248 / R:0x2ec88f8>
['Mean']
  <no name>: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x452dfc8 / R:0x3ad1018>
[80.000000, 95.000000]
  <no name>: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x452de18 / R:0x457de88>
[1.000000, 2.000000, 3.000000]
  ...
  <no name>: <type 'rpy2.rinterface.RNULLType'>
  rpy2.rinterface.NULL
  <no name>: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x452dd88 / R:0x457ddb0>
[2.000000, 2.000000, 2.000000]
  <no name>: <class 'rpy2.robjects.vectors.FloatVector'>
  <FloatVector - Python:0x45316c8 / R:0x457dd68>
[-1.000000, 0.000000, 1.000000]

I have done a similar example using rpy2 as follow: 我使用rpy2做了类似的例子如下:

    x = [1,2,3,4,1,2,3,4,1,2]
    v = robjects.FloatVector(x)
    t = robjects.r['ts'](v)
    fit = robjects.r['auto.arima'](t)
    next = robjects.r['forecast'](fit,h=1)

It is clear to know that I was doing a simple example of using arima to analysis time seires.When I got the next, I found that it was a ListVector.Then I used codes as follow to get the value I wanted. 很明显我知道我正在做一个使用arima来分析时间的简单例子。当我得到下一个时,我发现它是一个ListVector。然后我使用代码来获得我想要的值。

    count = len(next) - 2
    #ListVector->FloatVector->Float
    print next.rx('mean')[0][0]

Who knows whether this method is effective to you or not, just try it 谁知道这种方法对你有效,试试看

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

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