【发布时间】: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)
在网上搜索了几个小时后,我找不到任何帮助。 我猜洛朗有答案;-)
【问题讨论】:
如何使用 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)
在网上搜索了几个小时后,我找不到任何帮助。 我猜洛朗有答案;-)
【问题讨论】:
使用属性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