简体   繁体   English

将数据从R网格化为python并再次返回到R.

[英]Reticulate data from R to python and back to R again

Where am I going wrong here? 我在哪里错了? I am in RStudio and I want to do some processing on some text data in Python and bring it back to R for some final analysis / plots but I get the error: 我在RStudio,我想对Python中的一些文本数据进行一些处理,然后将它带回R进行最终的分析/绘图,但是我得到了错误:

NameError: name 'df_py' is not defined NameError:未定义名称'df_py'

Data & Code: 数据和代码:

text <- c("Because I could not stop for Death -",
              "He kindly stopped for me -",
              "The Carriage held but just Ourselves -",
              "and Immortality")

    ID <- c(1,2,3,4)

    df <- data.frame(cbind(ID, text))


    library(reticulate)

    df_py <- r_to_py(df)

    repl_python()

    df_py_clean['text'] = df_py['text'].str.replace("[^a-zA-Z]", " ")
    df_py_clean['text'] = df_py['text'].str.lower()
    exit

Once we are in the python REPL , use r. 一旦我们在python REPL ,使用r. to access the object 访问该对象

library(reticulate)
df_py <- r_to_py(df)
repl_python()
>>> r.df_py
#  ID                                    text
#0  1    Because I could not stop for Death -
#1  2              He kindly stopped for me -
#2  3  The Carriage held but just Ourselves -
#3  4                         and Immortality

and now do the transformation 现在进行转型

>>> r.df_py['text'] = r.df_py['text'].str.replace("[^a-zA-Z]", " ")
>>> r.df_py['text'] = r.df_py['text'].str.lower()
>>> exit

call the object from R R调用对象

df_py
# ID                                    text
#0  1    because i could not stop for death  
#1  2              he kindly stopped for me  
#2  3  the carriage held but just ourselves  
#3  4                         and immortality

NOTE: Not clear when the df_py_clean object was created. 注意:不清楚何时创建df_py_clean对象。 So, instead of that, here we are updating the same object column from python 所以,在这里,我们从python更新相同的对象列

NOTE2: The reverse to access python objects from R environment would be py$ 注2:从R环境访问python对象的反向是py$

data 数据

text <- c("Because I could not stop for Death -",
              "He kindly stopped for me -",
              "The Carriage held but just Ourselves -",
              "and Immortality")
ID <- c(1,2,3,4)
df <- data.frame(ID, text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM