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

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

我正在尝试从 python 调用 R 函数,但由于某些我还不知道的原因,当我将数字转换为 R 而不是矩阵(Dataframe)到R

例如,考虑下面的简单例子

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

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

转换器 numpy array -> R array needs 需要数组的维度。这里有none:

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