Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correlation of one variable to all the other in R

Tags:

r

correlation

I want to calculate the correlation between my dependent variable y and all my x. I use the code below,

   cor(loan_data_10v[sapply(loan_data_10v, is.numeric)],use="complete.obs")

the result is a correlation matrix. How can i just get one column with my variable y.

like image 633
Pumpkin C Avatar asked Aug 26 '17 05:08

Pumpkin C


People also ask

How do you find the correlation between all variables in R?

In this method to compute the correlation between all the variables in the given data frame, the user needs to call the cor() function with the entire data frame passed as its parameter to get the correlation between all variables of the given data frame in the R programming language.

Can you correlate 4 variables?

A correlation is usually tested for two variables at a time, but you can test correlations between three or more variables.

Can you correlate two continuous variables?

Correlation is a statistical method used to assess a possible linear association between two continuous variables. It is simple both to calculate and to interpret.


1 Answers

If we are looking for cor between 'x' and 'y', both argument can be either a vector or matrix. using a reproducible example, say mtcars and suppose 'y' is 'mpg' and 'x' the other variables ('mpg' is the first column, so we used mtcars[-1] for 'x')

cor(mtcars[-1], mtcars$mpg) 
#          [,1]
#cyl  -0.8521620
#disp -0.8475514
#hp   -0.7761684
#drat  0.6811719
#wt   -0.8676594
#qsec  0.4186840
#vs    0.6640389
#am    0.5998324
#gear  0.4802848
#carb -0.5509251

If we have numeric/non-numeric columns, create an index of numeric columns ('i1'), get the names of 'x' and 'y' variables using this index and apply the cor

i1 <- sapply(loan_data_10v, is.numeric)
y1 <- "dep_column" #change it to actual column name
x1 <- setdiff(names(loan_data_10v)[i1], y1)
cor(loan_data_10v[x1], loan_data_10v[[y1]])
like image 62
akrun Avatar answered Oct 31 '22 09:10

akrun