首先输入的文件类似如下图 为了转换为长数据,编写如下perl代码
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my $file=$ARGV[0];
my @matrix=();
my @month=();
my @year=();
open(RF,$file) || die $!;
open(WF,">tidy_data.txt") || die $!;
my $i=0;
while (my $line=<RF>){
next if ($.==1);
chomp($line);
my @arr=split('\t',$line);
shift(@arr);
for my $j (0..@arr-1){
$matrix[$i][$j]=$arr[$j];
}
$i++;
}
close(RF);
open(RF,$file) || die $!;
open(WFF,">process.txt") || die $!;
while(my $line=<RF>){
if ($.==1){
chomp($line);
@month=split('\t',$line);
next;
}
chomp($line);
my @arr=split('\t',$line);
print WFF $arr[0],"\n";
}
close(RF);
close(WFF);
open(RFF,"process.txt") || die $!;
while(my $line=<RFF>){
chomp($line);
push @year,$line;
}
close(RFF);
for my $i (0..$#year){
for my $j (0..$#month){
print WF $year[$i],"\t",$month[$j],"\t",$matrix[$i][$j],"\n";
}
}
close(WF);
system("del process.txt")
便可实现转换,结果如下图