[R] truth (karnaugh) table

Henrique Dallazuanna wwwhsd at gmail.com
Mon Sep 21 19:15:10 CEST 2009


Try this:

do.call(expand.grid, rep(list(0:1), 4))

On Mon, Sep 21, 2009 at 2:04 PM, baptiste auguie
<baptiste.auguie at googlemail.com> wrote:
> Dear list,
>
> I think I'm being dense, but I can't get combn or expand.grid to give
> me this result. I need to generate a matrix of all 16 possible
> sequences of 4 boolean elements,
>
> 0000
> 0001
> 0010
> 0011
> 0100
> .....
> 1111
>
> (in the end I'll have to assign NA to the 0s and some value to the 1s
> but let's keep it simple)
>
> OK, I could go the following route, but I feel like reinventing the
> wheel (and poorly),
>
>
> binarize <- function(x){
>  xt <- x
>  bin <- vector()
>  while(xt !=0){
>  bin <- c(bin, xt %% 2)
>  xt <- xt %/%2
> }
> c(rep(0, 4 - length(bin)), bin)
> }
>
> sapply(0:15, binarize)
>
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [,13] [,14] [,15] [,16]
> [1,]    0    0    0    0    0    0    0    0    0     1     0     1
>  0     1     0     1
> [2,]    0    0    0    0    0    1    0    1    0     0     1     1
>  0     0     1     1
> [3,]    0    0    0    1    0    0    1    1    0     0     0     0
>  1     1     1     1
> [4,]    0    1    1    1    1    1    1    1    1     1     1     1
>  1     1     1     1
>
>
> Have I missed a better way?
>
> Thanks,
>
> baptiste
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list