user2783615
user2783615

Reputation: 839

calculate correlation on every column

I want to calculate correlation of V2 with V3, V4, ..., V18: That is cor(V2,V3, na.rm = TRUE), cor(V2, V4, na.rm =TRUE), etc What is the most effective way to do this?

            V1    V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18
1 141_21311223 2.000  1  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
2  44_33331123 2.000  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
3 247_11131211 2.065  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
4  33_31122113 2.080  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
5 277_21212111 2.090  0  0  0  0  0  1  1   1   1   0   0   0   0   0   0   0

Upvotes: 0

Views: 138

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193687

Converting my comment to an answer, one simple approach would be to use the column positions in a sapply statement:

sapply(3:ncol(mydf), function(y) cor(mydf[, 2], mydf[, y], ))

This should create a vector of the output value. change sapply to lapply if you prefer a list as the output.

I've never seen na.rm for cor though....

Upvotes: 2

Related Questions