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 documentatio
Solution 1:
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->Floatprintnext.rx('mean')[0][0]
Who knows whether this method is effective to you or not, just try it
Solution 2:
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 z a structure that you can access, e.g
z[0]
<ListVector-Python:0x452d908 / R:0x457c770>
[StrVe..., Float..., Float..., ..., RNULL..., Float..., Float...]
<noname>: <class 'rpy2.robjects.vectors.StrVector'><StrVector-Python:0x452d248 / R:0x2ec88f8>
['Mean']
<noname>: <class 'rpy2.robjects.vectors.FloatVector'><FloatVector-Python:0x452dfc8 / R:0x3ad1018>
[80.000000, 95.000000]
<noname>: <class 'rpy2.robjects.vectors.FloatVector'><FloatVector-Python:0x452de18 / R:0x457de88>
[1.000000, 2.000000, 3.000000]
...
<noname>: <type 'rpy2.rinterface.RNULLType'>
rpy2.rinterface.NULL
<noname>: <class 'rpy2.robjects.vectors.FloatVector'><FloatVector-Python:0x452dd88 / R:0x457ddb0>
[2.000000, 2.000000, 2.000000]
<noname>: <class 'rpy2.robjects.vectors.FloatVector'><FloatVector-Python:0x45316c8 / R:0x457dd68>
[-1.000000, 0.000000, 1.000000]
Post a Comment for "Converting A R Listvector To A Python List With Rpy2"