【发布时间】:2017-07-11 07:50:52
【问题描述】:
我正在尝试使用 rpy2 在 Python(2.7) 中编写 R 函数,并且我在 R factorwars 中有一个函数可以保存包含字符串 'reg' 的方法中每个元素的索引:
from rpy2.robjects import r, pandas2ri
import rpy2.robjects as rob
import pandas as pd
pandas2ri.activate()
rstring="""
function(methods){
factorvars=which(methods %in% 'reg')
}
"""
rfunc=rob.r(rstring)
new_data=rfunc(methods)
rstring 实际上有点长,我尝试在字符串之后使用 '%sin%' %% 以及 \%in%,但 Python 一直将其解释为 %i 并要求一个整数。关于如何在 Python 中转义 %in% 或在 R 中使用 % 编写类似函数的任何想法。
编辑:包含更多代码、导入和函数。大部分问题是当我更改代码时,R 代码会出错。 this_is_only_string('some string') 的任何 python 函数可能会覆盖格式?
%%in%% 给出错误信息:
<text>:9:41: unexpected 'in'
8: fact = c('polyreg', 'logreg')
9: factorvars=which(method_array %%in
【问题讨论】: