Python:RRuntimeError:错误(函数(数据= NA,dim = length(数据),dimnames = NULL):'dims'的长度不能为0

时间:2017-09-23 03:30:06

标签: python-3.x rpy2

我试图从python中调用R函数但由于某些我还不知道的原因,当我将数字转换为R而不是矩阵(Dataframe)转换为R时,我得到一个RRuntimeError

例如,请考虑以下简单示例

import rpy2.robjects as robjects, pandas as pd, numpy as np
from rpy2.robjects import r
from rpy2.robjects.numpy2ri import numpy2ri
from rpy2.robjects.packages import importr

df= pd.DataFrame(np.random.random((108, 2)), columns=['Number1','Number2'])
p = 3 

resi1=np.array(df, dtype=float)
p1=np.array(p, dtype=float)

r_resi = numpy2ri(resi1)
r_p1= numpy2ri(p1)

然后输出

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-1-7970a0b7c100> in <module>()
     11 
     12 r_resi = numpy2ri(resi1)
---> 13 r_p1= numpy2ri(p1)

D:\Anaconda3\lib\site-packages\rpy2\robjects\numpy2ri.py in numpy2ri(o)
     80         #FIXME: no dimnames ?
     81         #FIXME: optimize what is below needed/possible ? (other ways to create R arrays ?)
---> 82         res = rinterface.baseenv['array'](vec, dim=dim)
     83     # R does not support unsigned types:
     84     elif o.dtype.kind ==

 "u":

RRuntimeError: Error in (function (data = NA, dim = length(data), dimnames = NULL)  : 
  'dims' cannot be of length 0

我不确定这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

转换器numpy数组 - &gt; R数组需要数组的维度。这里没有:

>>> p1
array(3.0)
>>> p1.shape
()