0

I am writing a script for my mass spectrometric data.
I have the data in a Matrix named M1. Every sample is in his own column.

Sample of M1:

   S1  S2  S3 
1 100 200 500 
2 200 300 500
3 100 200 500

Now when I place de data in the matrix I can compare the columns by M1$S1==M1$S2. and same for other samples.

How do I make this code run automatically all the samples?
So that it compares 1 with 2 and 1 with 3 and computes further?

Maybe if it is possible is there a way to not get a true and false list but just the false samples?

Cath
  • 23,906
  • 5
  • 52
  • 86
  • 2
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Nov 04 '15 at 15:47
  • 1
    what do you call a "false sample" ? – Cath Nov 04 '15 at 15:56
  • For the other question look at the functions `any()` and `all()` . And after that eventually `which()` . – jogo Nov 04 '15 at 15:59
  • The false samples are the data points which differ from the reference strain/spectra. – Timo van Duijn Nov 04 '15 at 20:34

1 Answers1

1

One way using combn and apply:

Data

M1 <- as.matrix(read.table(header=F, text=' 100,200,500 
  200, 300, 500
  100, 200, 500', sep=','))

Solution:

#get all the column combinations
combs <- combn(1:ncol(M1), 2)

#the columns describe the column combinations
> combs
     [,1] [,2] [,3]
[1,]    1    1    2
[2,]    2    3    3

#then use apply to compare the columns
apply(combs, 2, function(x) M1[,x[1]] == M1[,x[2]])

Output:

      [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE FALSE
[3,] FALSE FALSE FALSE

For the output above each column represents the comparison between the two columns in the same order as combs.

LyzandeR
  • 37,047
  • 12
  • 77
  • 87