Madina Tultabayeva Madina Tultabayeva - 11 months ago 70
R Question

Correlation between every column in R

I have a data with 24 variables(columns) and 1000 rows. Columns represent AGE,SALARY,REGION,GENDER, etc.

I need to find correlation between each column, (AGE,SALARY), (AGE, REGION) (AGE,GENDER) etc.i.e. I need to gdet 23*24=552 correlations. Is there any way to make a cycle or something, and get all those correlations at once, rather than find it seperately 552 times?

Please help! I can't do it 552 times. There must be a way!

UPDATE: I think I got what I wanted by


and I got something like


AGE 1 NA 0.25
REGION 0.25 NA 1

etc. but now the problem is that it gives me NA's which I don't need. I tried this

> COR<-cor(mytest[sapply(mytest,is.numeric)],use="complete.obs")

but unfortunately it doesn't work, gives me error "no complete element pairs". How do I do that?
Thanks in advance

Answer Source

I think you want a correlation matrix, try this:



I think I misunderstood, if you want to correlate Age with every other column try this:

apply(yourdataframe, 2, cor, x = AGE)