[R] truth (karnaugh) table
    baptiste auguie 
    baptiste.auguie at googlemail.com
       
    Mon Sep 21 19:04:14 CEST 2009
    
    
  
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
    
    
More information about the R-help
mailing list