Home > Net >  Listing ALL combinations of Factors
Listing ALL combinations of Factors

Time:12-29

Given the following data frame:

data_frame = data.frame(food = c("pizza", "tacos", "nachos"), drinks = c("water", "coffee", "pop"))

 data_frame
    food drinks
1  pizza  water
2  tacos coffee
3 nachos    pop

Is there a way to list ALL combinations of these factors?

For example:

food_combinations = c("none", pizza", "tacos", "nachos", "pizza & tacos", "pizza & nachos", "tacos & nachos", "pizza & tacos & nachos")

drink_combinations = c("none", coffee", "pop", "water", "coffee & pop", "coffee & water", "pop & water", "coffee & pop & water")

Thanks!

CodePudding user response:

We can use combn to do - loop over the columns with lapply, then do a nested loop over the sequence of the elements, apply combn and paste

lst1 <- lapply(data_frame, \(x) c("none", unlist(lapply(seq_len(length(x)),
        \(i) combn(x, i, FUN = paste, collapse = " & ")))))

-output

> lst1
$food
[1] "none"                   "pizza"                  "tacos"                  "nachos"                 "pizza & tacos"          "pizza & nachos"        
[7] "tacos & nachos"         "pizza & tacos & nachos"

$drinks
[1] "none"                 "water"                "coffee"               "pop"                  "water & coffee"       "water & pop"          "coffee & pop"        
[8] "water & coffee & pop"

CodePudding user response:

-Edit: adopted the response by @akrun as it is more concise and doesn't rely on any packages

Here is an alternative way, in the form of a function. Parameter m allows you to choose the maximum elements in each phrase.

df <- data.frame(
  food = c("pizza", "tacos", "nachos"),
  drinks = c("water", "coffee", "pop")
)

comb1 <- function(vector, m = length(vector)) {
  if (m >= length(vector)) {
    data <- unlist(lapply(seq_len(length(df$food)),
                          \(i) combn(df$food,i, FUN= paste, collapse=' & ')))
  }
  else {
    data <- unlist(lapply(seq_len(m),
                  \(i) combn(df$food,i, FUN= paste, collapse=' & ')))
  }
  return(data)
}

Which renders

> comb1(df$food)

[1] "none"                   "nachos"                 "pizza"                 
[4] "tacos"                  "nachos & pizza"         "nachos & tacos"        
[7] "pizza & tacos"          "nachos & pizza & tacos"

> comb1(df$food,2)

[1] "none"           "nachos"         "pizza"          "tacos"          "nachos & pizza"
[6] "nachos & tacos" "pizza & tacos" 

For a list over the dataframe then just a lapply

> lapply(df, comb1)

$food
[1] "none"                   "nachos"                 "pizza"                 
[4] "tacos"                  "nachos & pizza"         "nachos & tacos"        
[7] "pizza & tacos"          "nachos & pizza & tacos"

$drinks
[1] "none"                 "coffee"               "pop"                 
[4] "water"                "coffee & pop"         "coffee & water"      
[7] "pop & water"          "coffee & pop & water"

> lapply(df, \(x) comb1(x,2))

$food
[1] "none"           "nachos"         "pizza"          "tacos"          "nachos & pizza"
[6] "nachos & tacos" "pizza & tacos" 

$drinks
[1] "none"           "coffee"         "pop"            "water"          "coffee & pop"  
[6] "coffee & water" "pop & water"  
  • Related