Dataframe作为一个二维的数据结构,实现python与R的交互,能够更好的通过python来调用R的脚本哦

1.python 形成dataframe

from pandas import DataFrame
aa = DataFrame()
xs = [1,2,3,4,5,6]
....
aa['loc_x'] = xs
aa['loc_y']= ys
aa['pheno_type']= phenos
aa['id_shapes']= ids_shape

2.python与R的dataframe的交互–rpy2

方法一:

import rpy2.robjects as robjects
## get a reference to the R function 
write_csv = robjects.r('write.csv')
## save 
write_csv(df_R,'filename.csv')


#x=c(6,1,4,5,5,1)
#y=c(1,5,4,2,6,6)
#z=c(1,1,1,1,2,2)
#xy =c ("XX","YY","XY","XX","XY","YY")
#aa

方法二

from rpy2.robjects import pandas2ri
pandas2ri.activate()

from rpy2.robjects.packages import importr

base = importr('base')
# call an R function on a Pandas DataFrame
base.summary(my_pandas_dataframe)

参考资料:
http://stackoverflow.com/questions/24094476/python-pandas-to-r-dataframe
http://www.cnblogs.com/CQ-LQJ/p/4909920.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注