[R] Is there a Truth Table Generator in R?

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Sun Mar 13 20:33:28 CET 2022


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 using dcn.davis.ca.us>
To: Bert Gunter <bgunter.4567 using gmail.com>; Ebert, Timothy Aaron <tebert using ufl.edu>
Cc: r-help using r-project.org <r-help using r-project.org>; Paul Bernal <paulbernal07 using 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 using gmail.com> wrote:
>...
>tt$truth <- tt$A & tt$B & tt$C
>to evaluate the outcome of expand.grid.
>
>or, as I said,
>tt$truth <- apply(tt,1, all)
>which works for any number of columns in tt.
>
>
>Bert Gunter
>
>"The trouble with having an open mind is that people keep coming along
>and sticking things into it."
>-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>On Sat, Mar 12, 2022 at 2:02 PM Ebert,Timothy Aaron <tebert using ufl.edu> wrote:
>>
>> To the end of Jeff's program add
>> tt$truth <- tt$A & tt$B & tt$C
>> to evaluate the outcome of expand.grid.
>>
>> Tim
>>
>> -----Original Message-----
>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Newmiller
>> Sent: Saturday, March 12, 2022 12:05 PM
>> To: r-help using r-project.org; Paul Bernal <paulbernal07 using gmail.com>; R <r-help using r-project.org>
>> Subject: Re: [R] Is there a Truth Table Generator in R?
>>
>> [External Email]
>>
>> both <- c( FALSE, TRUE )
>> tt <- expand.grid( C = both
>>                  , B = both
>>                  , A = both
>>                  )
>> tt <- tt[, 3:1 ]
>>
>> On March 12, 2022 8:42:28 AM PST, Paul Bernal <paulbernal07 using gmail.com> wrote:
>> >Dear friends,
>> >
>> >Hope you are doing great. I have been searching for a truth table
>> >generator in R, but everything I find has a Python implementation instead.
>> >
>> >Maybe there is in fact a truth table generator in R, but I am not
>> >searching in the right places?
>> >
>> >Any help and/or guidance will be greatly appreciated.
>> >
>> >Best regards,
>> >Paul
>> >
>> >      [[alternative HTML version deleted]]
>> >
>> >______________________________________________
>> >R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
>> >an_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRz
>> >sn7AkP-g&m=Cj0cRxlu_GS0ARzvxm-eD27PhuhkgT_azaq1hamiYDmCglHF8_9hGTAkcDNo
>> >ZtUq&s=h0wdH7OvIxKWgjwFmBIGHvswAKy8VKwyyI3IbB9dKkc&e=
>> >PLEASE do read the posting guide
>> >https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
>> >_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsR
>> >zsn7AkP-g&m=Cj0cRxlu_GS0ARzvxm-eD27PhuhkgT_azaq1hamiYDmCglHF8_9hGTAkcDN
>> >oZtUq&s=tsrpB1zmIQL_wMcn70xPEkvpaisBrAM9k2OQ8kDrebw&e=
>> >and provide commented, minimal, self-contained, reproducible code.
>>
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=Cj0cRxlu_GS0ARzvxm-eD27PhuhkgT_azaq1hamiYDmCglHF8_9hGTAkcDNoZtUq&s=h0wdH7OvIxKWgjwFmBIGHvswAKy8VKwyyI3IbB9dKkc&e=
>> PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=Cj0cRxlu_GS0ARzvxm-eD27PhuhkgT_azaq1hamiYDmCglHF8_9hGTAkcDNoZtUq&s=tsrpB1zmIQL_wMcn70xPEkvpaisBrAM9k2OQ8kDrebw&e=
>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.

-- 
Sent from my phone. Please excuse my brevity.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list