hig*_*dth 5 python r list vector rpy2
在R中,我可以轻松地将元素添加到列表中:
mylist = list()
mylist[[1]] = c(1,2)
mylist[[2]] = c(2,3)
mylist[[length(mylist)+1]] = c(3,4)
Run Code Online (Sandbox Code Playgroud)
我如何在rpy2中执行此操作?我正在使用rpy2 2.1.9.我尝试了以下但它不起作用
import rpy2.robjects as robjects
a = robjects.r('list()')
b = robjects.IntVector([1,2])
a[0] = b
IndexError: Index out of range.
a[1] = b
IndexError: Index out of range.
aa = a.__add__(b) # But this makes a list out of the vector
aa.r_repr()
'list(1L, 2L)'
# We wanted something like the following instead:
aaa = robjects.r('list(c(1,2))')
aaa.r_repr()
'list(c(1, 2))'
Run Code Online (Sandbox Code Playgroud)
如果你想使用 R 的“[[<-”运算符,你必须调用方法 rx2() (参见 [赋值,R 风格][1])。
在 rpy2-2.2.0dev 中,您可以执行a.rx2[1] = b.
[1]:http://rpy.sourceforge.net/rpy2/doc-2.2/html/vector.html#assigning-r-style分配,R 风格