My question is simple, I am trying to transport one data point an integer from python to R. The reason for this is because I want to use R functions in R, create the output then back-transform them to python for plotting.
I have done this with arrays it work fine but not with one point array. Consider the simple example.
This below example work fine
import rpy2.robjects as robjects, pandas as pd, numpy as np
from rpy2.robjects import r
from rpy2.robjects.numpy2ri import numpy2ri
from rpy2.robjects.packages import importr
r_p1= numpy2ri(np.array((1,1)))
No problem! However if r_p1= numpy2ri(np.array((1)))
instead of r_p1= numpy2ri(np.array((1,1)))
I receive the following error
---------------------------------------------------------------------------
RRuntimeError Traceback (most recent call last)
<ipython-input-5-d404ef525bdb> in <module>()
4 from rpy2.robjects.packages import importr
5
----> 6 r_p1= numpy2ri(np.array((1)))
7
8 # df= pd.DataFrame(np.random.random((108, 2)), columns=['Number1','Number2'])
D:\Anaconda3\lib\site-packages\rpy2\robjects\numpy2ri.py in numpy2ri(o)
80 #FIXME: no dimnames ?
81 #FIXME: optimize what is below needed/possible ? (other ways to create R arrays ?)
---> 82 res = rinterface.baseenv['array'](vec, dim=dim)
83 # R does not support unsigned types:
84 elif o.dtype.kind == "u":
RRuntimeError: Error in (function (data = NA, dim = length(data), dimnames = NULL) :
'dims' cannot be of length 0
Just to ask my question better, as I understood from there is no way one may use one integer rather than an array in R.
But what I really need is to call a function in R that takes in input one of them is a single numeral such as 3, or maybe 21, elements that are determined in python.
In particular, I am calling such a library in R r('library("vars")')
Whereby I want to use some matrix call it y 2 by 100
# Run a normal VAR model
r("t=VAR(y, p=p1, type='const')")
as you may see this p1 is a single number say 3 (which is selected in python)
as r.assign("p1", r_p1)
d
# Then find the summary statistics
r('s=summary(t)')
this gives me this error
RRuntimeError: Error in crossprod(resids)/obs : non-conformable arrays
np.array((1, 1)) # tuple of length 2
np.array((1)) # literally the integer 1
np.array((1,)) # tuple with a single element
calling it on the third example np.array((1,))
worked for me
This is kind of weird, because there’s no such thing as a plain number in R; everything is a vector (sometimes of length 1). As far as I could tell from documentation, this is the best way to send an integer over.