0

I already referenced the post here. converting a Python list to an R numeric vector

I got the same error, but the provided solution does not work for me, so I am posting it. I am using python 2.7 and rpy2 corresponding newest version in Linux

I tried to call R function from python using rpy2, my first a few lines are as follows.

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

r=robjects.r
astsa=importr('astsa')
astsa.acf2(L, 3)       # L:a numeric list returned by my omitted code,I checked 

the error is as follows:

Traceback (most recent call last):
  File "/home/jin/Desktop/main.py", line 63, in <module>
    astsa.acf2(traffic[0], 3)
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 86, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/rpy2/robjects/functions.py", line 35, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
RRuntimeError: Error in stats::acf(series, max.lag, plot = FALSE) : 'x' must be numeric
Community
  • 1
  • 1
Jin
  • 1,203
  • 4
  • 20
  • 44
  • The error is coming from R: it expects a numerical vector and L is probably a list (looking at the error message from R) – lgautier Jun 12 '13 at 17:06
  • This is true, but when I try to convert python list to R vector using previous reference link, it did not go through, that is why I post – Jin Jun 12 '13 at 18:07
  • It would be easier to explain what is happening if you could provide a self-contained example (here there is a "I am doing things here, but everything I am doing is correct" gap in the middle, that does suggest that it contains the source and explanation of the problem) – lgautier Jun 12 '13 at 19:34
  • you can add one assignment line before the last line, say L=[1,2,3,4,5,6], the error messages remain. make sure you have astsa R package installed. Thanks – Jin Jun 12 '13 at 19:53
  • OK, I got it, you need one more line first to call R function L = robjects.FloatVector(L) – Jin Jun 12 '13 at 20:08

1 Answers1

-2

You can export easily a data frame generated with panda on Python:

import numpy as np              
import pandas as pd  

Create a data frame and export it:

mydataFrame.to_csv("path/to/mydataFrame.csv")

and import the data frame with read.csv in R. The data$column will give you your list.

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39