简体   繁体   中英

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. The problem is that I can't find in the documentation how to convert from a ListVector into a regular Python object. 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:

[<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? Thanks.

I change your forecaster function (just last line), using cbind to get an R vector and not a 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

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:

    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.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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