I have found the Data::Table to be a nice module for these types of tasks. It borrows some ideas from R that are helpful for handling tabular data (it has an implementation of the melt and cast functions inspired by the R reshape module). You can accomplish your task with the following code (assuming that your data is saved in a file named data.csv). The author of Data::Table has also made additional documentation/information available here: https://sites.google.com/site/easydatabase/

Data in data.csv:

Month,Zone,Replicate,SpeciesA,SpeciesB,SpeciesC Sept,1,1,5,10,15 Sept,1,2,0,5,10 Sept,1,3,5,0,5 Sept,2,1,5,5,5 Sept,2,2,10,15,10 Sept,2,3,0,0,5
#!/usr/bin/env perl use strict; use warnings; use Data::Table; use Statistics::Lite qw(mean stddev); my $dt = Data::Table::fromCSV("data.csv"); print "Original Data Table\n"; print "===================\n"; print $dt->tsv; print "\n\n"; my $melt = $dt->melt(['Month', 'Zone', 'Replicate']); print "Melt Table\n"; print "==========\n"; print $melt->tsv; print "\n\n"; my $cast_mean = $melt->cast( ['Month', 'Zone'], 'variable', Data::Table::STRING, 'value', \&mean ); print "Cast (mean)\n"; print "===========\n"; print $cast_mean->tsv; print "\n\n"; my $cast_stddev = $melt->cast( ['Month', 'Zone'], 'variable', Data::Table::STRING, 'value', \&stddev ); print "Cast (stddev)\n"; print "=============\n"; print $cast_stddev->tsv; exit;
This code will give the following output:
Original Data Table =================== Month Zone Replicate SpeciesA SpeciesB SpeciesC Sept 1 1 5 10 15 Sept 1 2 0 5 10 Sept 1 3 5 0 5 Sept 2 1 5 5 5 Sept 2 2 10 15 10 Sept 2 3 0 0 5 Melt Table ========== Month Zone Replicate variable value Sept 1 1 SpeciesA 5 Sept 1 1 SpeciesB 10 Sept 1 1 SpeciesC 15 Sept 1 2 SpeciesA 0 Sept 1 2 SpeciesB 5 Sept 1 2 SpeciesC 10 Sept 1 3 SpeciesA 5 Sept 1 3 SpeciesB 0 Sept 1 3 SpeciesC 5 Sept 2 1 SpeciesA 5 Sept 2 1 SpeciesB 5 Sept 2 1 SpeciesC 5 Sept 2 2 SpeciesA 10 Sept 2 2 SpeciesB 15 Sept 2 2 SpeciesC 10 Sept 2 3 SpeciesA 0 Sept 2 3 SpeciesB 0 Sept 2 3 SpeciesC 5 Cast (mean) =========== Month Zone SpeciesA SpeciesB SpeciesC Sept 1 3.33333333333333 5 10 Sept 2 5 6.66666666666667 6.66666666666667 Cast (stddev) ============= Month Zone SpeciesA SpeciesB SpeciesC Sept 1 2.88675134594813 5 5 Sept 2 5 7.63762615825973 2.88675134594813

In reply to Re: Descriptive Stats from .csv file by kevbot
in thread Descriptive Stats from .csv file by korsmo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.