使用 rpy2 将 NULL 从 Python 转换为 R [英] Convert NULL from Python to R using rpy2

查看:65
本文介绍了使用 rpy2 将 NULL 从 Python 转换为 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 R 中通常使用 NULL 值作为默认值.使用 Python 和 RPy2,如何显式提供 NULL 参数?

In R often the NULL value is used as default. Using Python and RPy2, how can one explicitly provide a NULL argument?

None 不可转换 (NotImplementedError),字符串 'NULL' 只会被转换为字符串并在执行过程中导致错误.

None is not convertible (NotImplementedError), a string 'NULL' will just be converted to a string and result in an error during execution.

以使用 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 然后调用该函数.例如:

You can use the r object to import as.null and then call that function. For example:

from rpy2.robjects import r

as_null = r['as.null']
is_null = r['is.null']
print(is_null(as_null())) #prints TRUE

我没有尝试你的代码,因为它的所有依赖关系,但如果 as_null 如上定义,你应该可以使用

I didn't try your code because of all of its dependencies, but if as_null is defined as above, you should be able to use

crost(ts, w=as_null())

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

Alternatively, use the NULL object definition directly from robjects:

import rpy2.robjects as robj

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

这篇关于使用 rpy2 将 NULL 从 Python 转换为 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆