Search code examples
rconditional-statementscombinationspermutationcombinatorics

Conditionally change value and finding combinations


I have a vector (testCol_1) consisting of 3 letters. Each letter is binary and can be replaced by either 0 or 1.

THE RULE is, if one letter (e.g. S) is replaced by a number (e.g. 1), all the S in the vector will also be replaced by that same number.

I would like to find all the possible combinations; for the following reproducible example, there are 8 possible combinations.

test <- expand.grid(rep(list(c("S","E","F")),4))
testCol_1 <- test$Var1

My solution:

This is my own solution but as you can see this is rather novice and I am quite sure there are better way to solve this.

# Changing to integer
testCol_1 <- as.integer(testCol_1)
# Finding all the combinations first
temp.combo <- expand.grid(rep(list(0:1),3))
test.final.result <- list()
for (i in 1:nrow(temp.combo)){
  testSol <- replace(testCol_1, testCol_1 == 1,temp.combo[i,1])
  testSol <- replace(testSol, testSol == 2,temp.combo[i,2])
  testSol <- replace(testSol, testSol == 3,temp.combo[i,3])
  test.final.result[[i]] <- testSol
}

Solution

  • Here is a solution with no loops, use R's vectorized instructions to get the 0/1 directly from temp.combo, by subsetting it, run the following to see it:

    temp.combo[, testCol_1]
    

    Note also that nvals is not strictly needed, it only makes the code more readable.

    test <- expand.grid(rep(list(c("S","E","F")), 4))
    testCol_1 <- as.integer(test$Var1)
    nvals <- length(unique(testCol_1))
    
    temp.combo <- expand.grid(rep(list(0:1), nvals))
    
    temp <- as.data.frame(t(temp.combo[, testCol_1]))
    test.final.result <- as.list(temp)
    test.final.result
    #> $V1
    #>  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    #> [39] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    #> [77] 0 0 0 0 0
    #> 
    #> $V2
    #>  [1] 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0
    #> [39] 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
    #> [77] 0 0 1 0 0
    #> 
    #> $V3
    #>  [1] 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
    #> [39] 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
    #> [77] 1 0 0 1 0
    #> 
    #> $V4
    #>  [1] 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1
    #> [39] 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1
    #> [77] 1 0 1 1 0
    #> 
    #> $V5
    #>  [1] 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
    #> [39] 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0
    #> [77] 0 1 0 0 1
    #> 
    #> $V6
    #>  [1] 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
    #> [39] 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1
    #> [77] 0 1 1 0 1
    #> 
    #> $V7
    #>  [1] 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1
    #> [39] 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
    #> [77] 1 1 0 1 1
    #> 
    #> $V8
    #>  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    #> [39] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    #> [77] 1 1 1 1 1
    

    Created on 2022-09-30 with reprex v2.0.2