[R] Pack and Unpack Strings in R

Gundala Viswanath gundalav at gmail.com
Fri Jan 9 16:50:20 CET 2009


Dear all,

Does R has any function/package that can pack
and unpack string into bit size?

The reason I want to do this in R is that R
has much more native statistical function than Perl.

Yet the data I need to process is so large that it
required me to compress it into smaller unit -> process it -> finally
recover them back again into string with new information.

In Perl the implementation will look like this:
I wonder how can this be implemented in R.

__BEGIN__
my %charmap = (
    A => '00',
    C => '01',
    G => '10',
    T => '11',
);

my %digmap = (
    '00'    => "A",
    '01'    => "C",
    '10'    => "G",
    '11'    => "T",
);

my $string = 'GATTA';
$string =~ s/(.)/$charmap{$1}/ge;

my $compressed = pack 'b*', $string;

print "COMP: $compressed\n";
printf "%d bytes\n", length $compressed;

my @data;

# Store the compressed bit into array
push @data, $compressed;

# process the array
foreach my $dat ( @data ) {

   my $decompressed = unpack 'b*', $dat;
   $decompressed =~ s/(..)/$digmap{$1}/ge;

   print "$decompressed\n";
   # or do further processing on $dat
}
__END__


- Gundala Viswanath
Jakarta - Indonesia




More information about the R-help mailing list