Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

multidimensional array in Statistics::R

by igelkott (Curate)
on Feb 07, 2013 at 13:37 UTC ( #1017656=perlquestion: print w/replies, xml ) Need Help??
igelkott has asked for the wisdom of the Perl Monks concerning the following question:

It's easy enough to load scalers and simple arrays into R through Statistics::R but I'm having trouble with multidimensional arrays. The manual doesn't actually claim to support this but maybe someone has managed.

Arrays are passed to R by reference (to make a simple list in R). Passing a reference to a multidimensional array produces a list of array tags. For example:

use Statistics::R; use Data::Dumper; my @arr = qw(foo bar baz); my @mat = (["fred", "barney", "pebbles", "bambam"], ["george", "jane", "elroy", "judy"], ["homer", "bart", "marge", "maggie"]); my $R = Statistics::R->new(); $R->set('arr', \@arr); $R->set('mat', \@mat); my $r_arr = $R->get('arr'); my $r_mat = $R->get('mat'); print "Array:\n", Dumper($r_arr); print "Matrix:\n", Dumper($r_mat);
Array: $VAR1 = [ 'foo', 'bar', 'baz' ]; Matrix: $VAR1 = [ 'ARRAY(0x7fdbf4007328)', 'ARRAY(0x7fdbf4014b80)', 'ARRAY(0x7fdbf4014c58)' ];

Replies are listed 'Best First'.
Re: multidimensional array in Statistics::R
by BrowserUk (Pope) on Feb 07, 2013 at 14:03 UTC

    Looking at the docs, it seems like you'd have to set one vector of the multi-dimensional array at a time.

    Something (untested) like this:

    ... $R->set("mat[$_]", $mat[$_] ) for 0 .. $#mat;

    But you probably need to tell R that 'mat' is a matrix first by preceding the above with something like:


    Or perhaps a data.frame with named columns would be a better choice. See the R docs.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I ended up splitting my data into individual vectors and then combining them in R. Yes, it'd be nice to load directly into a "data.frame" but it's not too much bother to do it on the R side.

      This whole thing was just because my data was so nicely packaged from the database (via fetchall_arrayref) and it seemed a shame to split it just to stitch it back together.

      Thanks. Good to see that it wasn't just some obvious thing I'd missed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017656]
Approved by toolic
[Lady_Aleena]: And I forgot to do the array check, I'm such a doofus today.
[Lady_Aleena]: push @line, ref($list_addition ) eq 'ARRAY' ? @$list_addition : $list_addition if $list_addition; #trying again
[shmem]: Discipulus: yummy. I like those. Didn't have them for some time now, forgot the name. Should go get some...
[shmem]: Lasy_Aleena: correct, although for clarity I'd use an if() block, not a statement modifier
[shmem]: this allows you to add an else if maintainance makes it neccesary
[Lady_Aleena]: shmem, okeydokey.
[shmem]: ...or logging or debugging or such

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-04-27 11:51 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (504 votes). Check out past polls.