如何将R中保存为RData的字符向量导入到python列表中?

时间:2017-07-29 19:20:03

标签: python r rpy2

如何将R中保存为RData的字符向量导入到python列表中?

例如,如果我在R:

中保存了这个字符向量
x <- c("Hello", "world!")
save (x, file = 'x.RData')

然后使用rpy2加载到python中,如下所示:

from __future__ import print_function
from rpy2.robjects import pandas2ri,r
import rpy2.robjects as robjects

def main():
    r['load']('x.RData')
    variables = tuple(robjects.globalenv.keys())
    print('variables: {0}'.format(variables))
    x = robjects.globalenv['x']
    print('x: {0}'.format(x))
    print('type(x): {0}'.format(type(x)))


if __name__ == "__main__":
    main()

打印:

variables: ('x',)
x: [1] "Hello"  "world!"
type(x): <class 'rpy2.robjects.vectors.StrVector'>

如何将其导入python列表?

1 个答案:

答案 0 :(得分:0)

您只需使用pointer-events

进行投射即可
list()