如何将保存为 RData 的字符向量从 R 导入到 python 列表?
How can I import a character vector from R saved as RData to a python list?
如何将保存为 RData 的字符向量从 R 导入到 python 列表?
例如,如果我在 R 中保存了这个字符向量:
x <- c("Hello", "world!")
save (x, file = 'x.RData')
然后在python中加载rpy2
如下:
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 列表?
你可以简单地使用 list()
:
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)))
print('list(x): {0}'.format(list(x)))
if __name__ == "__main__":
main()
如何将保存为 RData 的字符向量从 R 导入到 python 列表?
例如,如果我在 R 中保存了这个字符向量:
x <- c("Hello", "world!")
save (x, file = 'x.RData')
然后在python中加载rpy2
如下:
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 列表?
你可以简单地使用 list()
:
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)))
print('list(x): {0}'.format(list(x)))
if __name__ == "__main__":
main()