0

I'm relatively new in R so excuse me if I'm not even posting this question the right way.

I have a matrix generated from combination function.

double_expression_combinations <- combn(marker_column_vector,2)

This matrix has x columns and 2 rows. Each column has 2 rows with numbers that will be used to represent column numbers in my main data frame named initial. These columns numbers are combinations of columns to be tested. The initial data frame is 27 columns (thousands of rows) with values of 1 and 0. The test consists in using the 2 numbers given by double_expression_combinations as column numbers to use from initial. The test consists in adding each row of those 2 columns and counting how many times the sum is equal to 2.

I believe I'm able to come up with the counting part, I just don't know how to use the data from the double_expression_combinations data frame to select columns to test from the "initial" data frame.

Edited to fix corrections made by commenters

jesusgarciab
  • 129
  • 11
  • Welcome to SO. Please read [mcve], then edit your question to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Rich Scriven Jul 12 '16 at 18:43
  • `combn` produces a matrix, not a data.frame, which will make a rather large difference for this question. – alistaire Jul 12 '16 at 18:46

1 Answers1

0

Using R it's important to keep your terminology precise. double_expression_combinations is not a dataframe but rather a matrix. It's easy to loop over columns in a matrix with apply. I'm a bit unclear about the exact test, but this might succeed:

 apply( double_expression_combinations, 2,  # the 2 selects each column in turn
          function(cols){ sum( initial[ , cols[1] ] + initial[ , cols[2] ] == 2) } )

Both the '+' and '==' operators are vectorised so no additional loop is needed inside the call to sum.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • You got it! Thank you. I will also need to do the same but with combinations of 3 as in a triple_expression_combinations matrix. If I do apply( triple_expression_combinations, 2, function(cols){ sum( initial[ , cols[1] ] + initial[ , cols[2] ] + initial[ , cols[3] ] == 3) } ) Should work right? @42- – jesusgarciab Jul 12 '16 at 20:46