https://blog.csdn.net/lingan_hong/article/details/78911195
前面写到 《python3调用R》这篇文章。我们成功的搭建了python下调用R的环境。
下来来探讨一下,如何调用R包来处理python里面的数据。
我们把脚本装再r_script 里面并,用引号标注起来,然后就可以使用啦。
In [14]: r_script = '''
...: library(randomForest) # 导入随机森林包
...: ## use data set iris
...: data = iris # 使用鸢尾花数据集
...: table(data$Species)
...: ## create a randomForest model to classfy the iris species
...: # 创建随机森林模型给鸢尾花分类
...: iris.rf <- randomForest(Species~., data = data, importance=T, proximity=T)
...: print('--------here is the random model-------')
...: print(iris.rf)
...: print('--------here is the names of model-----')
...: print(names(iris.rf))
...: confusion = iris.rf$confusion
...: print(confusion)
...: '''
In [16]: robjects.r(r_script)
Out[16]:
R object with classes: ('matrix',) mapped to:
<Matrix - Python:0x00000000126E8A88 / R:0x000000000FD016A0>
[50.000000, 0.000000, 0.000000, ..., 0.000000, 0.060000, 0.060000]