在R中,NULL
值通常用作默认值。使用Python和RPy2,如何明确提供NULL
参数?
None
不可转换(NotImplementedError
),字符串'NULL'将被转换为字符串并在执行期间导致错误。
使用tsintermittent
包获取以下示例:
import numpy as np
from rpy2.robjects.packages import importr
from rpy2.robjects import numpy2ri
numpy2ri.activate()
tsintermittent = importr('tsintermittent')
crost = tsintermittent.crost
ts = np.random.randint(0,2,50) * np.random.randint(1,10,50)
# implicit ok, default w is NULL
# crost(ts)
# explicit - how to do it?
# RRunTimeError - non numeric argument
crost(ts, w='NULL')
# NotImplementedError
crost(ts, w=None)
您可以使用r
对象导入as.null
然后调用该函数。例如:
from rpy2.robjects import r
as_null = r['as.null']
is_null = r['is.null']
print(is_null(as_null())) #prints TRUE
我没有尝试你的代码,因为它的所有依赖项,但如果as_null
定义如上,你应该能够使用
crost(ts, w=as_null())
或者,直接使用NULL
中的robjects
对象定义:
import rpy2.robjects as robj
print(is_null(robj.NULL)) #prints TRUE
这个问题可能是因为NULL
的引用。无法检查rpy2
(因为有安装问题)。下面是与pyper
合作的代码
from pyper import *
import numpy as np
r=R(use_pandas=True)
ts = np.random.randint(0,2,50) * np.random.randint(1,10,50)
r('library(tsintermittent)')
r.assign('ts1', ts)
r('out <- crost(ts1, w = NULL)')
out1 = r.get('out')
out1.keys()
#['frc.out', 'initial', 'weights', 'components', 'model', 'frc.in']
无法用rpy2
测试问题。可能是反引号可能是一个选择,因为它是原样
crost(ts, w = `NULL`)