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

Erich Subscriptions er|ch@@ub@ @end|ng |rom neuw|rth@pr|v@@t
Sun Mar 13 13:51:04 CET 2022


I played around and dame up with the following raw idea for truth tables


library(tidyverse)


truth_table_inputs <- 
  function(n){
  if (n==1) return(
    tibble(x1=c(FALSE,TRUE)))
  expand_grid(truth_table_inputs(n-1),
              last_var=c(FALSE,TRUE)) ->
    res
  names(res) <- paste0("x",1:n)
  res              
}


```{r}
eval_truth_table <-
  function(n,fun){
    truth_table_inputs(n) |>
      bind_cols(
    truth_table_inputs(n) |>
      rowwise() |>
      (\(x)do.call(fun,as.list(x)))()
      ) -> res
    names(res)[n+1] <-
      deparse(substitute(fun))
    res
  } 

Example

eval_truth_table(3,function(x1,x2,x3)x1&x2|x3)

If there are more inputs than the function consumes, use dots

eval_truth_table(3,function(x1,x2,...)x1&x2)



> On 13.03.2022, at 10:17, Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
> 
> 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.



More information about the R-help mailing list