gpt4 book ai didi

r - 使用 rpy2 将 NULL 从 Python 转换为 R

转载 作者:行者123 更新时间:2023-12-05 02:12:39 24 4
gpt4 key购买 nike

在 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())

或者,直接从robjects使用NULL对象定义:

import rpy2.robjects as robj

print(is_null(robj.NULL)) #prints TRUE

关于r - 使用 rpy2 将 NULL 从 Python 转换为 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com