r - 使用 rpy2 将 NULL 从 Python 转换为 R
问题描述
在 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
推荐阅读
- angular - Angular 8:如何将点击事件发送到特定坐标
- vue.js - 将 v-model 附加到在 Vue.js 中使用 .appendChild 添加的动态元素
- flutter - 将日期格式化为月数和年数
- kotlin - Proguard 移除 lambda 调用
- python - 熊猫重塑列
- android - 为什么在android设备中运行项目时会发生这种与firebase相关的flutter错误?
- node.js - TypeError:无法使用 Express 读取 Node js 中未定义的属性“名称”
- python - pygame.quit() 在切换文件时给出 init.font 错误
- mysql - 针对 ActiveRecord 中的另一个数组查询 mysql json 数组
- openedge - 如何动态更改每个循环的 where 条件?