1

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 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, I will try it and see what happens. fn calculates a prespecified tetrachoric correlations. So I wanted to create a 500x500 correlation matrix for all the row variables in the data frame. I will try it and see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.