etricaen has asked for the wisdom of the Perl Monks concerning the following question:

hello, i have a hash table and i want transform this into an output which i could use in a R script. i use Text::table but i cant find a solution to have a good column separator (tab or pipe) this my code :

my %hash_sample_cds_genotype; my %hash_sample_cds_RT; my %hash_sample_cds_R1; my %hash_sample_cds_R2; #String des fichiers inputs foreach my $fichier (@fichiers) { my $sample_name; #alimentation du hash de resultat avec cette echatillon open(RESULTAT,"<$fichier") or die ("Cannot open $fichier\n"); while (defined(my $_ = <RESULTAT>)) { $sample_name = $fichier; my @attribut_name = split(m/out_/,$sample_name); $sample_name = $attribut_name[1]; my @resutat_infos = split(m/\t/,$_); next if ($_ =~ m/^CDS/); chomp($_); my $cds = $resutat_infos[0]; my $genotype = $resutat_infos[1]."/".$resutat_infos[2]; my $reads_p1 =$resutat_infos[3]; my $reads_p2 =$resutat_infos[4]; my $reads_totaux =$resutat_infos[6]; $hash_sample_cds_genotype{$sample_name}{$cds} = $genotype ; $hash_sample_cds_RT{$sample_name}{$cds} = $reads_p1 ; $hash_sample_cds_R1{$sample_name}{$cds} = $reads_p2 ; $hash_sample_cds_R2{$sample_name}{$cds} = $reads_totaux ; } close(RESULTAT); } #alimentation des fichier de resultats GENOTYPE my @cols1 = sort keys (%hash_sample_cds_genotype ); my @rows1 = sort keys % { { map { my $x = $_; map { $_ => undef } keys $hash_sample_cds_genotype{$x} } @cols1 } }; my $out1 = Text::Table->new(' ', @cols1); for my $r1 (@rows1) { $out1->add($r1, map $hash_sample_cds_genotype{$_}{$r1} // 'NA', @c +ols1); } print OUTPUT1 $out1;

Any idea ?

  • Comment on transform a hash table into R dataframe or matrix with tab separator
  • Download Code

Replies are listed 'Best First'.
Re: transform a hash table into R dataframe or matrix with tab separator
by GotToBTru (Prior) on Mar 30, 2016 at 15:36 UTC

    For output, you may want to use the table() method. See the documentation. I scanned through it to see if there was a way to specify how the columns are separated, but I did not see anything.

    You might want to look into Text::ASCIITable which has more options. UPDATE: see also Data::Frame.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      CPAN Weekly recommended Text::Table::Tiny on February the 28th.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        thx but i can find a good example to make work this module with a hash :(