python - rpy2:在放入 DataFrame 时防止 StrVector 分解

标签 python r rpy2

rpy2 , 我注意到 StrVector一旦它被放入 DataFrame 中就会被分解.示例如下。

import rpy2.robjects as ro

series_1 = ("0", "0", "0", "0")
series_1_robject = ro.StrVector(series_1)  # => ['0', '0', '0', '0']
df = ro.DataFrame({"series_1": series_1_robject})    # => FactorVector [1, 1, 1, 1]

还有……

>>> df[0][1]
1

看来,当我构建一个 DataFrame ,我的好StrVector被分解,所以 0对应因子值1 (R 是 1 索引的),依此类推。但是我该如何阻止这种情况发生呢?当输入向量 ( series_1 ) 是 0,0,0...,0 时,这对我来说非常重要。 , 它在结果中的表示 DataFrame将是 0 , 不是 1 .到目前为止,我还没有真正能够在文档中找到关于此事的任何内容....

最佳答案

根据注释 here ,您可以通过调用 ro.r.I() (the "as-is" function in R) 包装 StrVector 来阻止此转换为 FactorVector :

In [1]: import rpy2.robjects as ro

In [2]: series_1 = ("0", "0", "0", "0")

In [3]: series_1_robject = ro.StrVector(series_1)

In [4]: df = ro.DataFrame({"series_1": series_1_robject})

In [5]: df.rx2("series_1")
Out[5]:
R object with classes: ('factor',) mapped to:
<FactorVector - Python:0x113a39368 / R:0x7f8d15882e40>
[       1,        1,        1,        1]

In [6]: df = ro.DataFrame({"series_1": ro.r.I(series_1_robject)})

In [7]: df.rx2("series_1")
Out[7]:
R object with classes: ('AsIs',) mapped to:
<StrVector - Python:0x113a398c0 / R:0x7f8d13a8aec8>
[str, str, str, str]

关于python - rpy2:在放入 DataFrame 时防止 StrVector 分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598284/

相关文章:

python - 如何在 Python 3 中创建具有大指数的 Decimal?

sql-server - sql保存: Mapping dataframe timestamps to SQL Server timestamps

python - 为什么 Rmagic 会反转单元格中绘图的顺序?

python - 使用 rpy2 将 R 包安装/导入到 python 中,导入/忽略有问题的包

python - 当我执行 GET 请求时(在 Python 中),我得到了翻译的文本。如何获取英文内容?

python - 将python中的整数解析为另一个软件的命令

python - 更改标签文本时出现问题

r - 从列表中选择多个元素以构建数据框的最快方法

r - R : Leaving out undefined columns when subsetting a dataframe

Python rpy2 和 matplotlib 在使用 multiprocessing 时发生冲突