Search code examples
rcombinationspermutationcalculated-columns

Adding column elements conditionally in R


Reproducible example:

set.seed(1)
testMat <- matrix(round(runif(3*6,1,5)), nrow = 3, ncol = 6)

Output:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    2    5    5    1    4    3
[2,]    2    2    4    2    3    4
[3,]    3    5    4    2    4    5

Here first 3 columns (1,2,3) belong to one set and next 3 columns (4,5,6) belong to another set. I want to add one column from each set and need to do it for all possible combinations.

For this example, I should get 9 resulting vectors as there are 9 combinations:

Combination 1: (1,4) = (3,4,5)
Combination 2: (1,5) = (6,5,7)
Combination 3: (1,6) = (.,.,.)
Combination 4: (2,4) = (.,.,.)
Combination 5: (2,5) = (.,.,.)
Combination 6: (2,6) = (.,.,.)
Combination 7: (3,4) = (.,.,.) 
Combination 8: (3,5) = (.,.,.)
Combination 9: (3,6) = (.,.,.)

Is there any elegant way to do it, especially when the number of columns can be higher? For example 9, 12, etc. would yield combinations of 27 and 81 respectively.

EDIT: More clarification: Each 3 columns (e.g. 1:3, 4:6, 7:9, 10:12,...etc.) construct one set and the objective is to take 1 column from each set and add them together. For example if we have 6 columns at testMat we take 1 column among 1:3 and another from 4:6 add tese 2 columns together. Similarly for 9 we add 3, for 12 we add 4 columns one from each set.


Solution

  • Here is one way :

    #Create a sequence from 1 to number of columns in the data
    cols <- seq(ncol(testMat))
    n <- 3
    #Create a list of every n columns and use expand.grid 
    #to create all possible combinations
    vals <- expand.grid(split(cols, ceiling(cols/n)))
    #RowSums every combination using `sapply`.
    sapply(asplit(vals, 1), function(x) rowSums(testMat[, x]))
    
    #     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
    #[1,]    3    6    6    6    9    9    5    8    8
    #[2,]    4    4    6    5    5    7    6    6    8
    #[3,]    5    7    6    7    9    8    8   10    9