Search code examples
rfunctionoperatorsvenn-diagram

Function for logical table


So I have this function for logical (venn diagrams) calculation but I cant make function universal for any dataframe of any size..

This function only work for provided dataframe (only four columns)


how_much = 5000000
A <- sample(how_much, replace = TRUE, x = 1:5) 
B <- sample(how_much, replace = TRUE, x = 1:5)
C <- sample(how_much, replace = TRUE, x = 1:5)
D <- sample(how_much, replace = TRUE, x = 1:5)

VennData = data.table(A, B, C, D)


Venn_Counts <- function(dataset, unique_number, operator) {
  message("Operator arrgument are: `==` or`<` or `<=` or `>` or `>=`")
  if(inrange(unique_number, 1, 35) ){
    dataset %>% as_tibble() %>% 
      mutate(A = (operator(A, unique_number)),
             B = (operator(B, unique_number)),
             C = (operator(C, unique_number)), 
             D = (operator(D, unique_number))) %>%
      count(A, B, C, D)
  }
  else {
    print("Unique number must be in range from 1 to 5")
  }
}


Venn_Counts(VennData, 2, operator = `<=`)

how would we make above function universal for dataframe that would have more columns?

for smaller objects we would get something like : arguments setting is unique_number = 3, operator = ==

count    A      B
 24     TRUE   TRUE
 20     TRUE   FALSE
 13     FALSE  TRUE
 43     FALSE  FALSE

when we can see that we have 24 observations where both A and B is equal to 3, 20 observations have A equal to 3 and B non equal to 3, 13 observations have A not equal to 3 and B equal to 3 etc...


Solution

  • How about using the scoped verbs from dplyr:

    library(data.table)
    library(dplyr)
    
    how_much = 5000000
    A <- sample(how_much, replace = TRUE, x = 1:5) 
    B <- sample(how_much, replace = TRUE, x = 1:5)
    C <- sample(how_much, replace = TRUE, x = 1:5)
    D <- sample(how_much, replace = TRUE, x = 1:5)
    
    VennData = data.table(A, B, C, D)
    
    
    Venn_Counts <- function(dataset, unique_number, operator) {
      message("Operator arrgument are: `==` or`<` or `<=` or `>` or `>=`")
      if(inrange(unique_number, 1, 35) ){
        dataset %>% 
          as_tibble() %>% 
          mutate_all( ~ operator(.x, unique_number)) %>%
          group_by_all() %>% 
          count()
      }
      else {
        print("Unique number must be in range from 1 to 5")
      }
    }
    
    
    Venn_Counts(VennData, 2, operator = `<=`)