Skip to content
Prev 390990 / 398521 Next

Is there a Truth Table Generator in R?

After slogging through lots of posts about a poorly defined request, I am left wondering if I missed the original sender properly explaining what THEY mean by a truth table and what it should look like.
Some here seem to know (or are guessing) that the request is to make all combinations of TRUE and FALSE for N columns in a data.frame and some for an indefinite value of N. Some others may also want to throw in additional columns that reflect a logical AND operation and perhaps others.
So I calmly request someone tell us what the real request is so I can evaluate if anything said here makes much sense in answering the real request.
As I see it, if you have 2 columns, there are four possible combination in what amounts to a 4x2 matrix. If your mailer allows my text to be seen as intended, the following shows combinations starting with F, albeit a table starting with T is equivalent in terms of meaning:
FFFTTFTT

For an N=3 column it gets more rows using binary notation with T=0 and F=1 so 8 rows.
000001010011100101110111

The trend becomes clear that the number of rows is 2**N power so a simple approach (albeit there are other ways shown that may be simpler to code using existing software) is to note the pattern. The first column requires 2**N items alternating every (2**N)/2 times. Meaning if N=5 then you want 32 rows in the result with 16 units of F and then 16 units of T, or vice versa. The R function that does this easily (as part of a loop perhaps) is rep() and sample code (hopefully blank lines keep it from getting wrapped funny is something like this that can be simplified:
N <- 5
rows <- 2**N
TF <- data.frame(index=1:rows)
for (ind in rev(2**(N:1))) {??? TF <- cbind(TF, rep(c(TRUE, FALSE), each=rows/ind, length.out=rows))??}
names(TF) <- c("index", paste("col", 1:N, sep=""))
The above uses rep() repeatedly to produce runs of TRUE and FALSE of decreasing size and keeps concatenating them to an existing data.frame with cbind(). The result is a column with 16 TRUE followed by 16 FALSE then another column with 8 by 8 and repeated again as 8 by 8. The next column alternates in groups of 4 then the next in groups of two and finally alternating in "groups" of 1.
Obviously this can be wrapped up in a function that takes N as an argument and makes an arbitrary N column construct with 2**N rows as described and this may be what is wanted for the main table. I threw this together rapidly and I am sure can improve it so column names are created as appropriate.
For example, rather than cbind, the following would work well too:
colnm <- ...TF[colnm] <-?rep(c(TRUE, FALSE), each=rows/ind, length.out=rows)

But the question is whether this makes what is wanted, or needs something more like columns that represent whether the OR or AND or some other boolean function of N boolean items is TRUE or FALSE.
I repeat, the above analysis does not suggest other supplied solutions are bad or wrong, just a suggestion of how fairly simple functionality in R can do what is wanted. Of course, if something else is wanted, we are all wasting our time answering. I waited a while hoping not to need to or to reply to an actual question I know how to deal with.
-----Original Message-----
From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
To: Bert Gunter <bgunter.4567 at gmail.com>; Ebert, Timothy Aaron <tebert at ufl.edu>
Cc: r-help at r-project.org <r-help at r-project.org>; Paul Bernal <paulbernal07 at gmail.com>
Sent: Sun, Mar 13, 2022 5:17 am
Subject: Re: [R] Is there a Truth Table Generator in R?

There are 2^(2^length(tt)) possible "truth" vectors for the inputs defined in tt. AND-ing all of the inputs only gives one of those possibilities. Some popular named cases for 2 inputs are shown here [1], but it is common to use combinations of !, & and | to specify a particular truth vector. There is also the problem of reverse-engineering such a boolean expeession [2] in simplest form from a given truth vector, but I don't know if anyone has implemented such algorithms in R.

[1] https://en.wikipedia.org/wiki/Truth_table

[2] https://en.wikipedia.org/wiki/Karnaugh_maps
On March 12, 2022 2:17:32 PM PST, Bert Gunter <bgunter.4567 at gmail.com> wrote: