 |
Forums |
Admin Start New Thread
By: Alex Gutteridge
RE: RSRuby and matrices [ reply ] 2007-04-06 01:35
|
HI Andrew,
As you've noticed, matrices aren't directly supported by the default RSRuby conversion scheme. Since this is a fairly fundamental part of R it would be good to improve this and I've added matrix support to my to-do list.
For now, there are a number of ways to handle this depending on what you want to do. The simplest is to turn off conversion in the matrix method:
require 'rsruby'
r = RSRuby.instance
r.matrix.autoconvert(RSRuby::NO_CONVERSION)
m = r.matrix([1,2,3,4],:nrow=>2,:ncol=>2)
puts r.is_matrix(m)
r.fisher_test(m)
The Ruby object 'm' is now an RObj representing your matrix and you should be able to pass this to the fisher.test method. Actually, on my machine calling fisher.test causes a segfault so there's another bug somewhere, but at least we've got a usable matrix object.
A more complete solution would be to use the proc conversion mode and a custom class to intercept the matrix conversions:
require 'rsruby'
require 'matrix'
class Matrix
def as_r
"wibble"
end
end
r = RSRuby.instance
test_proc = lambda{|x| RSRuby.instance.is_matrix(x)}
conv_proc = lambda{|x| Matrix[*x.to_ruby]}
r.proc_table[test_proc]=conv_proc
r.matrix.autoconvert(RSRuby::PROC_CONVERSION)
m = r.matrix([1,2,3,4],:nrow=>2,:ncol=>2)
puts m.class
puts r.is_matrix(m)
The code above correctly converts an R matrix into a Ruby standard library Matrix. But, we need to define the Matrix as_r method correctly to get the conversion working the other way though. I'll have a think about that over the next few days and try and get back to you. I'll also try and have a look at that segfault with the fisher.test method.
AlexG
|
By: Nobody
RSRuby and matrices [ reply ] 2007-04-05 20:25
|
Hi There,
I wondering if there is an easy way to deal with matrices using RSRuby. I'm having problems with the conversion to "list" objects. For example:
irb(main):065:0> r.assign('m',r.matrix([1,2,3,4],nrow=2,ncol=2))
=> [[1, 3], [2, 4]]
irb(main):066:0> r.is_matrix(r.m)
=> false
irb(main):067:0> r.typeof(r.m)
=> "list"
How can I maintain the matrix object in R? For example, if I wanted to do fisher's exact tests, etc.
many thanks,
Andrew
|
|
 |