Rpy2中的分类变量(因子函数)

时间:2014-03-29 14:39:12

标签: python r rpy2 logistic-regression

如何在rpy2中执行此操作? mydata是一个数据帧,秩是从1到4的变量

mydata$rank <- factor(mydata$rank)

1 个答案:

答案 0 :(得分:0)

根据这个http://rpy.sourceforge.net/rpy2/doc-2.3/html/vector.html#dataframe,你必须以间接的方式做到这一点:

In [51]:

import pandas as pd
import rpy2.robjects as ro
import pandas.rpy.common as py2r
In [52]:

DF=pd.DataFrame({'val':[1,1,1,2,2,3,3]})
In [53]:

r_DF = py2r.convert_to_r_dataframe(DF)
In [54]:

print r_DF
  val
0   1
1   1
2   1
3   2
4   2
5   3
6   3

In [55]:

from rpy2.robjects.vectors import DataFrame
r_DF2=DataFrame({'factor': ro.r['factor'](r_DF.rx2('val')),
                 'val': r_DF.rx2('val')})
In [56]:

print r_DF2
  val factor
1   1      1
2   1      1
3   1      1
4   2      2
5   2      2
6   3      3
7   3      3