标签

2017年1月31日星期二

Perl: Combine columns

1. combine columns according column names

#!/usr/bin/perl -w
use strict;

my (@name, @A, @B, @all);

@name = split /\s+/, <DATA>; # skip first line;
print "$name[0]\tA\tB\n";

for (0 .. $#name) {
    push(@A, $_), next if $name[$_] =~ /^A/; # save array indices;
    push(@B, $_), next if $name[$_] =~ /^B/;
}

while (<DATA>) {
    chomp;
    @all = split;
    my $A_start = $A[0];
    my $A_end = $A[-1];
    my $A_sum = 0;
    map {$A_sum += $_} @all[$A_start..$A_end];
    my $A_mean = $A_sum/@A;
    print "$all[0]\t$A_mean\t";
    
    my $B_start = $B[0];
    my $B_end = $B[-1];
    my $B_sum = 0;
    map {$B_sum += $_} @all[$B_start..$B_end];
    my $B_mean = $B_sum/@B;
    print "$B_mean\n";
}


__DATA__
name A1  A2  A3  B1  B2  B3  B4  ……
a    1   2   3   4   4   4   8   ……
b    6   3   9   12  16  12  32  ……
c    9   33  12  16  20  24  24  ……
d    9   9   15  28  24  16  32  ……

Output:

name A B
a 2 5
b 6 18
c 18 21
d 11 25

2016年4月5日星期二

Perl: The first and last lines

1. Get the first and last lines

#!/usr/bin/perl -w
use strict;

print scalar <DATA>; # the first line
while (<DATA>) {
    print if eof; # the last line after loop
}


__DATA__
first
second
third
lastline

Output:

first
lastline

2. Get the first and last lines

#!/usr/bin/perl -w
use strict;

my $last;
my $first = <DATA>; # the first line

while (<DATA>) {
    $last = $_;  # the last line after loop
}
close DATA;

print "$first";
print "$last";


__DATA__
first
second
third
lastline

3. Skip the first line

readline <DATA>;
scalar <DATA>;
my $first = <DATA>;
@all = split /\s+/, <DATA>;

 

 

 

 

 

 

2016年4月2日星期六

Adobe Acrobat

配置:

编辑——首选项——辅助工具——忽略页面显示——总是使用页面布局样式——单页连续

                                                                                   总是使用缩放设置——适合宽度

 

2016年3月30日星期三

Statistics: Error bars, T-test & ANOVA

1. Error bars are used to show the distribution of data, indicate the error, or uncertainty in a reported measurement, and give a general idea of how precise a measurement is.

image

Descriptive error bars give information describing the data and show how the data are spread.

Inferential error bars are used to conclude whether the groups are significantly different, or whether the differences might just be due to random fluctuation or chance.

image

It is highly suggested using larger n, to achieve narrower inferential error bars and more precise estimates of true population values.

image

Statistic hypothesis test

It is used to test the significance of group differences between two or more groups.

• T test (Student test)

looks at differences between two groups on some variable of interest

• ANOVA (Analysis of variance)

tests the significance of group differences between two or more groups

2. T test

image

image

Paired two sample means: from same samples

Two-sample assuming equal variances: from different samples with equal variances

Two-sample assuming unequal variances: from different samples with unequal variances

image

image

3. ANOVA

image

image

image

image

image

image

image

image

 

image

2016年3月29日星期二

Perl: Combine rows

1. Combine rows

#!/usr/bin/perl -w
##########################################################################################
#Goal:combine 1st column and count max, min and mean values for each following columns
##########################################################################################
use strict;
use Data::Dumper;
use List::Util qw/max min sum/;

my (%rec, $key, @vals);

while (<DATA>) {
    chomp;
    ($key, @vals) = split;
    while (my ($i, $v) = each @vals) {
        push @{$rec{$key}->[$i]}, $v;
    }
}
print Dumper (\%rec);

while (my ($key, $vals) = each %rec) { #while & each to go through %hash 
    print $key, "\t";
    while (my ($i, $v) = each @vals) { #while & each to go through @array 
        my $max = max @{$rec{$key}->[$i]};
        my $min = min @{$rec{$key}->[$i]};
        my $sum = sum @{$rec{$key}->[$i]};
        my $n = @{$rec{$key}->[$i]};
        my $mean = $sum / $n;
        # 以下改变内部变量;
        $rec{$key}->[$i][0] = $max; # 求最大值;
        $rec{$key}->[$i][1] = $min; # 求最小值;
        $rec{$key}->[$i][2] = $mean; # 求平均值;
        print $rec{$key}->[$i][0]."/";
        print $rec{$key}->[$i][1]."/";
        print $rec{$key}->[$i][2]."\t";
    }
    print "\n";
}


__DATA__
a   2   3   1   3   2   2
s   2   2   2   2   2   3
s   1   2   3   1   2   1
b   3   2   2   1   5   2
a   2   3   3   5   2   4
s   6   8   4   9   2   5

Output:

$VAR1 = {
          'a' => [
                   [
                     '2',
                     '2'
                   ],
                   [
                     '3',
                     '3'
                   ],
                   [
                     '1',
                     '3'
                   ],
                   [
                     '3',
                     '5'
                   ],
                   [
                     '2',
                     '2'
                   ],
                   [
                     '2',
                     '4'
                   ]
                 ],
          'b' => [
                   [
                     '3'
                   ],
                   [
                     '2'
                   ],
                   [
                     '2'
                   ],
                   [
                     '1'
                   ],
                   [
                     '5'
                   ],
                   [
                     '2'
                   ]
                 ],
          's' => [
                   [
                     '2',
                     '1',
                     '6'
                   ],
                   [
                     '2',
                     '2',
                     '8'
                   ],
                   [
                     '2',
                     '3',
                     '4'
                   ],
                   [
                     '2',
                     '1',
                     '9'
                   ],
                   [
                     '2',
                     '2',
                     '2'
                   ],
                   [
                     '3',
                     '1',
                     '5'
                   ]
                 ]
        };
a 2/2/2 3/3/3 3/1/2 5/3/4 2/2/2 4/2/3 
b 3/3/3 2/2/2 2/2/2 1/1/1 5/5/5 2/2/2 
s 6/1/3 8/2/4 4/2/3 9/1/4 2/2/2 5/1/3 

2016年3月28日星期一

Perl: Array of array

1. Array of array

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my (@all, @aoa, @all2, @all3, @all34, @aoa2);

while (<DATA>) {
    chomp;
    @all = split;
    push @all34, $all[2], $all[3];
    push @aoa, [$all[2], $all[3]];
    push @all2, $all[2];
    push @all3, $all[3];
}
push @aoa2, (\@all2, \@all3);

print Dumper (@all34);
print Dumper (\@aoa);
print Dumper (\@aoa2);


__DATA__
a   1   2   3   4   5
b   3   5   6   6   8
c   1   5   5   5   7
d   4   2   6   7   9

Output:

$VAR1 = '2';
$VAR2 = '3';
$VAR3 = '5';
$VAR4 = '6';
$VAR5 = '5';
$VAR6 = '5';
$VAR7 = '2';
$VAR8 = '6';
$VAR1 = [
          [
            '2',
            '3'
          ],
          [
            '5',
            '6'
          ],
          [
            '5',
            '5'
          ],
          [
            '2',
            '6'
          ]
        ];
$VAR1 = [
          [
            '2',
            '5',
            '5',
            '2'
          ],
          [
            '3',
            '6',
            '5',
            '6'
          ]
        ];

2016年3月27日星期日

Perl: List::Util (转)

以下这些函数来自于 List::Util 模块,这是 Perl 内置的模块,不用白不用!

1. 求数组的和:不需要一个一个地累加,直接调用 sum 函数

use List::Util qw/sum/;
my @array = (10, 20, 30, 40);
my $sum = sum @array;       # 得到 100

2. 求数组的最大、最小值:不需要逐个比较,直接调用 max 和 min 函数

use List::Util qw/max min/;
my @array = (10, -1, 6, 25, 8);
my $max = max @array;           # 得到 25
my $min = min @array;           # 得到 -1

3. 如果是按照字符串排列的最大、最小值呢?调用 maxstr 和 minstr 函数

use List::Util qw/maxstr minstr/;
my @array = ("Beijing", "Shanghai", "Guangzhou", "Chengdu", "Nanjing");
my $maxstr = maxstr @array;     # 得到 Shanghai
my $minstr = minstr @array;     # 得到 Beijing

转自:http://bnuzhutao.cn/archives/788