rpy2

Rpy2: pandas dataframe can't fit in R

杀马特。学长 韩版系。学妹 提交于 2021-02-18 19:06:14
问题 I need to read a csv file with python (into a pandas dataframe), work in R and return to python. Then, to pass pandas dataframe to R dataframe I use rpy2, and work ok (code bellow). from pandas import read_csv, DataFrame import pandas.rpy.common as com import rpy2.robjects as robjects r = robjects.r r.library("fitdistrplus") df = read_csv('./datos.csv') r_df = com.convert_to_r_dataframe(df) print(type(r_df)) And this output is: <class 'rpy2.robjects.vectors.FloatVector'> But then, I try to

Convert NULL from Python to R using rpy2

不问归期 提交于 2021-02-10 12:06:40
问题 In R often the NULL value is used as default. Using Python and RPy2, how can one explicitly provide a NULL argument? None is not convertible ( NotImplementedError ), a string 'NULL' will just be converted to a string and result in an error during execution. Take the following example using the tsintermittent package: import numpy as np from rpy2.robjects.packages import importr from rpy2.robjects import numpy2ri numpy2ri.activate() tsintermittent = importr('tsintermittent') crost =

Convert NULL from Python to R using rpy2

南楼画角 提交于 2021-02-10 12:05:45
问题 In R often the NULL value is used as default. Using Python and RPy2, how can one explicitly provide a NULL argument? None is not convertible ( NotImplementedError ), a string 'NULL' will just be converted to a string and result in an error during execution. Take the following example using the tsintermittent package: import numpy as np from rpy2.robjects.packages import importr from rpy2.robjects import numpy2ri numpy2ri.activate() tsintermittent = importr('tsintermittent') crost =

Convert NULL from Python to R using rpy2

此生再无相见时 提交于 2021-02-10 12:04:04
问题 In R often the NULL value is used as default. Using Python and RPy2, how can one explicitly provide a NULL argument? None is not convertible ( NotImplementedError ), a string 'NULL' will just be converted to a string and result in an error during execution. Take the following example using the tsintermittent package: import numpy as np from rpy2.robjects.packages import importr from rpy2.robjects import numpy2ri numpy2ri.activate() tsintermittent = importr('tsintermittent') crost =

assign variables from python to R using r.assign and then use read.table

隐身守侯 提交于 2021-02-08 06:35:35
问题 I'm trying to use python to define a variable as a string, specifically, a path to a file. I then want python to pass that string to an R variable. I then want to use R's read.table function to write the contents of that file to the variable in R as a table. I'm using rpy2 and r.assign to accomplish this, but I'm getting no where. Any help would be appreciated! The error message I receive is pasted below the code. import os import sys from rpy2.robjects import r import rpy2.robjects as

rpy2 passing python reserved keyword arguments

自作多情 提交于 2021-02-07 20:25:57
问题 I am trying to use r's density function through python, and I have to pass the 'from', 'to' arguments to the density functions. However, since the word 'from' is a reserved ketyword in python, how can I achieve this? Thank you. Here is the code so far. r_density=robjects.r('density') f_a = robject.FloatVector(a) r_a = r_density(f_a, bw='SJ', n=1024) ## Here I need to add 'from' and 'to' arguments 回答1: You can use dict argument-unpacking to pass reserved words as parameter names: r_a = r

Module 'rpy2.robjects.pandas2ri' has no attribute 'ri2py'

爷,独闯天下 提交于 2021-02-07 13:39:06
问题 I'm trying to convert R-dataframe to Python Pandas DataFrame. I use the following code: from rpy2.robjects import pandas2ri pandas2ri.activate() r_dataframe = r_function(my_dataframe['Numbers']) print(r_dataframe) python_dataframe = pandas2ri.ri2py(r_dataframe) The above code works well in Jupyter Notebook (Anaconda). But if I run this code through a my_program.py file through the terminal, I get an error: :~$ python3 my_program.py Traceback (most recent call last): File "my_program.py", line

Module 'rpy2.robjects.pandas2ri' has no attribute 'ri2py'

爱⌒轻易说出口 提交于 2021-02-07 13:34:16
问题 I'm trying to convert R-dataframe to Python Pandas DataFrame. I use the following code: from rpy2.robjects import pandas2ri pandas2ri.activate() r_dataframe = r_function(my_dataframe['Numbers']) print(r_dataframe) python_dataframe = pandas2ri.ri2py(r_dataframe) The above code works well in Jupyter Notebook (Anaconda). But if I run this code through a my_program.py file through the terminal, I get an error: :~$ python3 my_program.py Traceback (most recent call last): File "my_program.py", line

Calling an R script with command line arguments from Python rpy2

不想你离开。 提交于 2021-02-07 09:12:56
问题 I want to be able to call R files from python using the rpy2 modules. I would like to be able to pass arguments to these scripts that can be interpreted by R's commandArgs function. So if my R script ( trivial_script.r ) looks like: print(commandArgs(TRUE)) and my Python 2.7 script looks like: >>> import rpy2.robjects as robjects >>> script = robjects.r.source('trivial_script.r') How can I call it from rpy2.robjects with the arguments "arg1", "arg2", "arg3" ? 回答1: I see the two approaches

Change R version when using rpy2 package in Jupyter notebook

痞子三分冷 提交于 2021-01-29 09:23:29
问题 I want to run a piece of R code with %%R in my python3-notebook. It works, but the problem is that the R version of the notebook is the 3.6.1, while the one in my RStudio is the 4.0.1. In fact when I run R.home() the paths are different. I tried to change the version using: os.environ['R_HOME'] = '/Library/Frameworks/R.framework/Resources' %load_ext rpy2.ipython but when I call it I have a lot of errors: /Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py