使用rpy2将NULL从Python转换为R.

问题描述 投票:2回答:2

在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 python-3.x null rpy2
2个回答
2
投票

您可以使用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

0
投票

这个问题可能是因为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`)
© www.soinside.com 2019 - 2024. All rights reserved.