[R] 2 questions regarding base-n and identifing digits + the source problem description

Martin Maechler maechler at stat.math.ethz.ch
Fri Oct 10 12:36:17 CEST 2003


>>>>> "Andrej" == Andrej Kveder <andrejk at zrc-sazu.si>
>>>>>     on Thu, 9 Oct 2003 10:09:22 +0200 writes:

    Andrej> Thanks for the suggestions..  The strsplit function
    Andrej> works great for the second question...  I will have
    Andrej> to come up with another solution to the first one,
    Andrej> since my problem would definetly involve a lot of
    Andrej> calculations and I think that current methods would
    Andrej> cause major computational congestion...  BUt here is
    Andrej> the outline of my problem... if anybody has an
    Andrej> idea...  In practice I start with 4 possible
    Andrej> correlations: -0.9, -0.1, 0.1, 0.9 and want to
    Andrej> construct all possible 3x3 correaltion matrices with
    Andrej> those values.  So I need to do all possible
    Andrej> permutations of a given length with replication. I
    Andrej> have a set of 4 distinct values and want to create
    Andrej> the permutations of length 3.  My idea was as
    Andrej> folows: specifing the sequence in base-4 form 0 to
    Andrej> 333 would account for all possible combinations.  I
    Andrej> checked some of the existing functions which were
    Andrej> either recursive and slow or without replication. I
    Andrej> think there was a thread on the list just a short
    Andrej> while ago.  If anybody has a hint to follow, i would
    Andrej> be more then glad to try it out...

So, what you really want is combinatorics and the are two
packages on CRAN that provide these.

Back to the original question: representing numbers in other
bases than 10 :

Several years ago, I had defined a "baseint" class (S3) and
methods for doing these base conversions -- for integers only.
I'm appending the R source file (baseint.R) with the
definitions.
As the header of the file says, I've always wanted to do this
in a much nicer way, but didn't get around to that.
actually, cbind.baseint() must have a bug.
In spite of all that it can be useful, e.g.

> (b1 <- baseint(0:10, 2))
 [1] 0{b}2    1{b}2    10{b}2   11{b}2   100{b}2  101{b}2  110{b}2  111{b}2 
 [9] 1000{b}2 1001{b}2 1010{b}2
> (o1 <- baseint(0:10, 8)) # octal : has special print form
 [1] 00  01  02  03  04  05  06  07  010 011 012
> (h1 <- baseint(0:17, 16))# hexadesimal
 [1] x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf  x10 x11
> c(b1) ## gives the char strings
 [1] "0"    "1"    "10"   "11"   "100"  "101"  "110"  "111"  "1000" "1001"
[11] "1010"
> c(baseint(1:64, 4))
 [1] "1"    "2"    "3"    "10"   "11"   "12"   "13"   "20"   "21"   "22"  
[11] "23"   "30"   "31"   "32"   "33"   "100"  "101"  "102"  "103"  "110" 
[21] "111"  "112"  "113"  "120"  "121"  "122"  "123"  "130"  "131"  "132" 
[31] "133"  "200"  "201"  "202"  "203"  "210"  "211"  "212"  "213"  "220" 
[41] "221"  "222"  "223"  "230"  "231"  "232"  "233"  "300"  "301"  "302" 
[51] "303"  "310"  "311"  "312"  "313"  "320"  "321"  "322"  "323"  "330" 
[61] "331"  "332"  "333"  "1000"
> 

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: baseint.R
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20031010/1c71727e/baseint.pl
-------------- next part --------------


Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><


More information about the R-help mailing list