Adding column elements conditionally in R

Viewed 90

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.

3 Answers

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

expand.grid can be surprisingly helpful:


nc <- ncol( testMat )

if( nc %% 3 != 0 ) {
  stop( "Your data's number of columns should be a multiple of 3!")
}

n <- ncol( testMat ) / 3

args <- sapply( 1:n, function(i) (i*3-2):(i*3), simplify=FALSE )

combs <- do.call( expand.grid, args ) %>% arrange( Var1 )

combs %>% apply( 1, function(r) {
              rowSums( testMat[, r] )
          })



Output:


> combs %>% apply( 1, function(r) {
+               rowSums( testMat[, r ] )
+           })
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    3    6    5    6    9    8    6    9    8
[2,]    4    5    6    4    5    6    6    7    8
[3,]    5    7    8    7    9   10    6    8    9

Here's an approach with to generate the permutations:

library(RcppAlgos)

my_vals = function (...) {
    dots = list(...)
    ncols = ...length()
    cols = permuteGeneral(3L, ncols, TRUE)
    
    Reduce(`+`, Map(`[`, dots, asplit(cols,2L)))
}

do.call('my_fun', asplit(array(testMat, c(3L, 3L, ncol(testMat) / 3L)), 3L))

This approach turns the matrix into a list of submatrices which are then subsetted by the permutations.

Related