1
votes

I have a file that has a matrix of 500 rows (binary scores) and 120 columns. The file is a simple matrix of 0s and 1s.

>file

00010010101010
01001010100101
00101001010001
11110101001010

I am writing a function that uses a special correlation formula to find this correlation between the rows. It takes two vector rows as input fn(row1, row2). eg. row1 and row2 and calculates this special correlation.

Example

>fn(file[1,], file[2,])
>0.32

I am able to do it for two rows but how can I create a 500x500 correlation matrix for all rows. Can someone please help with this? Thanks.

1

1 Answers

5
votes

Try this:

corr.mat <- outer(seq_len(nrow(file)), seq_len(nrow(file)),
                  Vectorize(function(i, j) fn(file[i,], file[j,])))

If this is too slow for your needs, there might be better approaches but you'll have to show what fn is supposed to do.

P.S.: file being the name of a function in R, you should avoid using it for your own variables.