将Python中的列表转换为R中的向量

时间:2013-06-12 16:38:40

标签: python r rpy2

我已经在这里引用了这篇文章。 converting a Python list to an R numeric vector

我遇到了同样的错误,但提供的解决方案对我不起作用,所以我发布了它。我在Linux中使用python 2.7和rpy2相应的最新版本

我尝试使用rpy2从python调用R函数,我的第一行如下。

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 

错误如下:

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

1 个答案:

答案 0 :(得分:0)

您可以轻松导出在Python上使用panda生成的数据框:

import numpy as np              
import pandas as pd  

创建数据框并导出它:

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

并在R中导入read.csv的数据框。data$column会为您提供列表。