Craig
Craig

Reputation: 69

How to find correlation between any combination of arrays

I have 10 data sets and I want to check the correlation between all possible pairs.For example, if I had:

ABCD

I want to check the correlation between AB, AC, AD, BC etc.

I've been using Correl function in excel which is fine for small data sets but if I had 1000 data sets instead of 10, how would I do this?

Upvotes: 0

Views: 155

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

This solution assumes you have datasets in your global environment and they can be "scraped" based on some criterion. In my case, I opted for ".string" handle. If not, you have to come up with your own way of putting names into a string. Another way would be to put all datasets into a list and work with indices.

A.string <- runif(5)
B.string <- runif(5)
C.string <- runif(5)

# find variables based on a common string
pairs <- combn(ls(pattern = "\\.string"), 2)

# for each pair, fetch variable and use function cor()
apply(pairs, MARGIN = 2, FUN = function(x) {
  cor(get(x[1]), get(x[2]))
})

[1] 0.2586141 0.7106571 0.7119712

Upvotes: 1

Related Questions