ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 Mar 2013 08:56:58 +0100Passing array from sage to R and back againhttps://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/Good Morning
I am trying to pass data from sage to R and then received the result back
y=asarray(y)
r.install_packages('"ismev"')
r.library('ismev')
gv=r.get_fit('y')
gv
I have tried various ways to do this but non of them work
Has anyone else managed to do this?
Thanks for any help
By the way to get install_packages to work you need to do
sage -f r
from a terminal after installing sage.
I think this could be added to the documentation.
Tue, 26 Feb 2013 05:10:13 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/Answer by fidbc for <p>Good Morning</p>
<p>I am trying to pass data from sage to R and then received the result back</p>
<pre><code>y=asarray(y)
r.install_packages('"ismev"')
r.library('ismev')
gv=r.get_fit('y')
gv
</code></pre>
<p>I have tried various ways to do this but non of them work
Has anyone else managed to do this?</p>
<p>Thanks for any help</p>
<p>By the way to get install_packages to work you need to do
sage -f r
from a terminal after installing sage.
I think this could be added to the documentation.</p>
https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?answer=14602#post-id-14602To convert a python list to an r array you can try
sage: x = r.as_array( range(10) )
sage: x
[1] 0 1 2 3 4 5 6 7 8 9
To convert back `x`'s `_sage_()` method might do the trick
sage: y = x._sage_()['DATA']
sage: y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tue, 26 Feb 2013 09:10:59 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?answer=14602#post-id-14602Comment by kcrisman for <p>To convert a python list to an r array you can try</p>
<pre><code>sage: x = r.as_array( range(10) )
sage: x
[1] 0 1 2 3 4 5 6 7 8 9
</code></pre>
<p>To convert back <code>x</code>'s <code>_sage_()</code> method might do the trick</p>
<pre><code>sage: y = x._sage_()['DATA']
sage: y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>
https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18154#post-id-18154I wasn't aware of the `as_array` - nice!Tue, 26 Feb 2013 09:22:27 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18154#post-id-18154Comment by kcrisman for <p>To convert a python list to an r array you can try</p>
<pre><code>sage: x = r.as_array( range(10) )
sage: x
[1] 0 1 2 3 4 5 6 7 8 9
</code></pre>
<p>To convert back <code>x</code>'s <code>_sage_()</code> method might do the trick</p>
<pre><code>sage: y = x._sage_()['DATA']
sage: y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>
https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18155#post-id-18155Somewhat related are http://ask.sagemath.org/question/807/converting-r-variables-to-sage and http://ask.sagemath.org/question/660/inter-mixing-sage-and-rTue, 26 Feb 2013 09:22:17 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18155#post-id-18155Comment by kcrisman for <p>To convert a python list to an r array you can try</p>
<pre><code>sage: x = r.as_array( range(10) )
sage: x
[1] 0 1 2 3 4 5 6 7 8 9
</code></pre>
<p>To convert back <code>x</code>'s <code>_sage_()</code> method might do the trick</p>
<pre><code>sage: y = x._sage_()['DATA']
sage: y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>
https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18126#post-id-18126Actually, it's always wanted just single quotes - we pass a string. Or is this a regression that it used to accept the single/double style and now doesn't?Fri, 01 Mar 2013 08:56:58 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18126#post-id-18126Comment by Thomas Ward for <p>To convert a python list to an r array you can try</p>
<pre><code>sage: x = r.as_array( range(10) )
sage: x
[1] 0 1 2 3 4 5 6 7 8 9
</code></pre>
<p>To convert back <code>x</code>'s <code>_sage_()</code> method might do the trick</p>
<pre><code>sage: y = x._sage_()['DATA']
sage: y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>
https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18128#post-id-18128Thanks for that fidelbc, perfect it solved the problem. The R tos age inerface is slow for passing lots of DATA but it did work. By the way I found that r.install_packages in the current sage release doesn't want the double quotes inside the single quotes, it just wants single quotes.Fri, 01 Mar 2013 08:52:14 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18128#post-id-18128Comment by kcrisman for <p>To convert a python list to an r array you can try</p>
<pre><code>sage: x = r.as_array( range(10) )
sage: x
[1] 0 1 2 3 4 5 6 7 8 9
</code></pre>
<p>To convert back <code>x</code>'s <code>_sage_()</code> method might do the trick</p>
<pre><code>sage: y = x._sage_()['DATA']
sage: y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</code></pre>
https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18153#post-id-18153One caveat (which is probably hard to avoid: `sage: type(y[1])` yields `<type 'int'>`, so you'd have to do a little more trickery to get Sage `Integer`s.Tue, 26 Feb 2013 09:23:58 +0100https://ask.sagemath.org/question/9851/passing-array-from-sage-to-r-and-back-again/?comment=18153#post-id-18153