English 中文(简体)
计算Mean的代号
原标题:Calculating the Mean from aPerl Script
  • 时间:2011-11-22 23:56:55
  •  标签:
  • statistics

I m still in here. ;) I ve got this code from a very expert guy, and I m shy to ask him this basic questions...anyway this is my question now; this Perl Script prints the median of a column of numbers delimited space, and, I added some stuff to get the size of it, now I m trying to get the sum of the same column. I did and got not results, did I not take the right column? ./stats.pl 1 columns.txt
#!/usr/bin/perl

use strict;
use warnings;

my $index = shift;
my $filename = shift;
my $columns = [];

open (my $fh, "<", $filename) or die "Unable to open $filename for reading
";

for my $row (<$fh>) {

my @vals = split/s+/, $row;
push @{$columns->[$_]}, $vals[$_] for 0 .. $#vals;
}

close $fh;

my @column = sort {$a <=> $b} @{$columns->[$index]};

my $offset = int($#column / 2);
my $length = 2 - @column % 2;

my @medians = splice(@column, $offset, $length);

my $median;
$median += $_ for @medians;
$median /= @medians;

print "MEDIAN = $median
";
################################################
my @elements = @{$columns->[$index]};
my $size = @elements;
print "SIZE = $size
";
exit 0;
#################################################
my $sum = @{$columns->[$index]};
for (my $size=0; $size < $sum; $size++)  { 
my $mean = $sum/$size;
};
print "$mean
";

预先感谢。

最佳回答

• 看到你去做的一些要点:

你们可以把所有数字纳入一个阵列:

my @result = split(m/d+/, $line);
#average 
use List::Util qw(sum);
my $sum = sum(@result);

然后,如果指数是你想要查阅的一栏数,则你可以查阅<代码>(单位:美元/代码>)。

2. 还注意到:

$total = $line + $total;
$count = $count + 1;

可以改写为:

$total += $line;
$count += 1;

最后,确保你阅读档案:

a. 将“de”印成正文:

print $line, "
";

这应当使你:

问题回答

暂无回答




相关问题
How to manage a pageview DB

I am interested in tracking my users pageviews on my site. Being that traffic is expanding very quickly, I am worried about robots, etc, and I also want to be able to use tracked data live to alter ...

Statistics Question

Suppose I conduct a survey of 10 people asking whether to rank a movie as 0 to 4 stars. Allowable answers are 0, 1, 2, 3, and 4. The mean is 2.0 stars. How do I calculate the certainty (or ...

Calculating variance with large numbers

I haven t really used variance calculation that much, and I don t know quite what to expect. Actually I m not too good with math at all. I have a an array of 1000000 random numeric values in the ...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Generating correlated numbers

Here is a fun one: I need to generate random x/y pairs that are correlated at a given value of Pearson product moment correlation coefficient, or Pearson r. You can imagine this as two arrays, array ...

Multivariate time series modelling in R

I want do fit some sort of multi-variate time series model using R. Here is a sample of my data: u cci bci cpi gdp dum1 dum2 dum3 dx 16.50 14.00 53.00 45.70 80....

热门标签