[R] multiple bases to decimal (was: comparing two matrices)

jim holtman jholtman at gmail.com
Sun Jan 21 15:30:23 CET 2007

```I think you are computing your bases in the wrong way.  If the data
represents 3 columns with base 3,3,2, then the multiplier has to be
c(6,2,1) not c(9,3,1).  I think this should compute it correctly:

# create a matrix of all combination of bases 3,3,2
mat1 <- expand.grid(0:1, 0:2, 0:2)[,3:1]
base <- c(3,3,2)  # define the bases
# now create the multiplier
mbase <-  c(rev(cumprod(rev(base))),1)[-1]
# show the data
mat1
base
mbase
# combine with original
cbind(mat1, conv=colSums(apply(mat1, 1, function(x) x*mbase)))

On 1/21/07, Adrian Dusa <dusa.adrian at gmail.com> wrote:
>
> Hi again,
>
> I was contemplating the solution using base 3:
> set.seed(3)
> mat2 <- matrix(sample(0:2, 15, replace=T), 5, 3)
>
> Extracting the line numbers is simple:
> bases <- c(3, 3, 3)^(2:0)       # or just 3^(2:0)
> colSums(apply(mat2, 1, function(x) x*bases)) + 1
> [1]  7 23 25  8  1
>
> The problem is sometimes the columns have different number of levels, as in:
> mat1 <- expand.grid(0:2, 0:2, 0:1)[,3:1]
>
> Is there any chance to combine different bases in order to obtain the
> corresponding line numbers?
> I thought of something like:
> bases <- c(3, 3, 2)^(2:0)
>
> but it doesn't work (sigh).
>
> Thanks for any hint,
>
> --
> Romanian Social Data Archive
> 1, Schitu Magureanu Bd
> 050025 Bucharest sector 5
> Romania
> Tel./Fax: +40 21 3126618 \
>          +40 21 3120210 / int.101
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

```