怎么在python里调用R

1.安装[windows]

conda install rpy2 

2.把R的路径添加在环境变量中

如果用pip install rpy2 报错了Tried to guess R's HOME but no R command in the PATH

2.具体调用可见教程

一般的例子【具体见https://yulongniu.bionutshell.org/blog/2012/08/21/python-r-rpy2/

from rpy2.robjects import r

r('''f <- function(r){pi * r}''')

r.f(3)

如果要导入R中的包

from rpy2.robjects.packages import importr 

r('''install.packages('hwriter')''')   #安装包
r('''
     hwriter:::hwrite.table(matrix(1:10, 2))
    ''')

3.备注

把Rdata.frame转化为pd.DataFrame https://stackoverflow.com/questions/20630121/pandas-how-to-convert-r-dataframe-back-to-pandas

from rpy2.robjects import pandas2ri

corr_df_in_R = pandas2ri.py2ri_pandasdataframe(pd.DataFrame(corr_matrix)) #py_df --> r_df

猜你喜欢

转载自blog.csdn.net/u_7890/article/details/84851573
今日推荐