Ask Your Question
1

Passing array from sage to R and back again

asked 2013-02-26 05:10:13 +0100

Thomas Ward gravatar image

updated 2013-02-26 09:22:58 +0100

kcrisman gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-02-26 09:10:59 +0100

fidbc gravatar image

To 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]
edit flag offensive delete link more

Comments

I wasn't aware of the `as_array` - nice!

kcrisman gravatar imagekcrisman ( 2013-02-26 09:22:27 +0100 )edit

One 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.

kcrisman gravatar imagekcrisman ( 2013-02-26 09:23:58 +0100 )edit

Thanks 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.

Thomas Ward gravatar imageThomas Ward ( 2013-03-01 08:52:14 +0100 )edit

Actually, 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?

kcrisman gravatar imagekcrisman ( 2013-03-01 08:56:58 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-02-26 05:10:13 +0100

Seen: 421 times

Last updated: Feb 26 '13