【问题标题】:How to set an attribute to a vector in rpy2如何在rpy2中将属性设置为向量
【发布时间】:2015-11-05 20:33:09
【问题描述】:

如何使用 rpy2 在 python 中为向量添加属性。 例如,如何重现此 R 代码:

library(evir)
pot<-c(2.0,3.2,4,5,6,7)
ts<-c(1,6,7,19,20,30)
attr(pot,"times")<-ts
output<-decluster(pot,run=2)

在网上搜索了几个小时后,我找不到任何帮助。 我猜洛朗有答案;-)

【问题讨论】:

    标签: python rpy2


    【解决方案1】:

    使用属性slots。 S4 对象的文档中关于它的描述适用于属性 (http://rpy2.readthedocs.org/en/version_2.7.x/notebooks/s4class.html)。

    这里应该可以使用:

    from rpy2.robjects.vectors import FloatVector, IntVector
    pot = FloatVector((2.0, 3.2, 4, 5, 6, 7))
    ts = IntVector((1,6,7,19,20,30))
    pot.slots['times'] = ts
    

    对于 rpy2

    pot.do_slot_assign("times",ts)
    

    【讨论】:

    • pip install rpy2 --upgrade
    • 在我的系统上并不容易 ;-) 您能否指定答案中所需的最低版本?谢谢
    • 可能是 2.7.0。在旧版本上,do_slot_assign 可能会让你到达那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    相关资源
    最近更新 更多