【问题标题】:Using PyRserve to call library(foreign) in Python在 Python 中使用 PyRserve 调用库(外部)
【发布时间】:2017-01-26 12:16:19
【问题描述】:

我正在尝试将 SPSS 文件加载到 Python 中的 Pandas DataFrame 中,并且正在从 Python 环境中使用 R 代码的最新发展中寻找更简单的方法来完成此任务,这导致我使用 PyRserve。

连接到 PyRserve 后,

import pyRserve
conn = pyRserve.connect()

几乎可以运行基本的 r 代码,例如

conn.eval('3+5') #output = 8.0

但是,如果可能在 PyRserve 中,如何导入一个 R 库来加载一个带有如下 r 代码的数据框,

library(foreign)
dat<-read.spss("/path/spss_file.sav", to.data.frame=TRUE)

并希望在 pandas DataFrame 上?任何想法表示赞赏!

【问题讨论】:

    标签: python r dataframe spss pyrserve


    【解决方案1】:
    #import pyRserve
    import pyRserve
    
    #open pyRserve connection
    conn = pyRserve.connect()
    
    #load your rscript into a variable (you can even write functions)
    test_r_script = '''
                    library(foreign)
                    dat<-read.spss("/path/spss_file.sav", 
                                     to.data.frame=TRUE)
                    '''
    
    #do the connection eval
    variable = conn.eval(test_r_script)
    
    print variable
    
    # closing the pyRserve connection
    conn.close()
    

    我很抱歉没有正确解释它...我正在添加我的 github 链接,以便您可以查看更多示例。我想我已经在那里正确解释了 https://github.com/shintojoseph1234/Rserve-and-pyRserve

    【讨论】:

    • 只需浏览 README.md 和 views.py。我实际上已经为 python/Django 项目创建了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多