简体   繁体   中英

creating a correlation matrix in R

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM