David Ghan
David Ghan

Reputation: 13

applying correlation to all columns in R

I am trying to apply a single function to find the correlation between all numeric columns and the target variable (separately, one by one)

This is the code I was able to determine a single column's correlation. I'm trying to limit my correlations above 0.4:

> if(abs(cor(train$YearBuilt, train$SalePrice)) > .4) {
+       print(abs(cor(train$YearBuilt, train$SalePrice)))
+     }
[1] 0.5228973

I would like to be able to print the column name followed by the correlation, and then the next column name and its correlation, etc.

Upvotes: 1

Views: 2158

Answers (2)

ericgtaylor
ericgtaylor

Reputation: 311

One possibility using dplyr. A little indulgent with the pipe, but it does your filtering by 0.4 and retains the variable names too.

> train = select(iris, -Species)
> head(train)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4
> train %>% 
+   summarize_all(funs(cor(., iris$Sepal.Length))) %>% 
+   t() %>% 
+   as.data.frame() %>% 
+   rownames_to_column("var") %>% 
+   rename(cors = V1) %>% 
+   filter(cors > 0.4)
           var      cors
1 Sepal.Length 1.0000000
2 Petal.Length 0.8717538
3  Petal.Width 0.8179411

Upvotes: 2

Adam Quek
Adam Quek

Reputation: 7153

Here's an example of finding the correlation of iris$Petal.Length with the other numerical variables:

vars <- c("Sepal.Length", "Sepal.Width", "Petal.Width")
all <- lapply(vars, function(i) list(x= iris[,i], y=iris[,"Petal.Length"]))
lapply(all, function(x) do.call(cor, x))

[[1]]
[1] 0.8717538

[[2]]
[1] -0.4284401

[[3]]
[1] 0.9628654

Upvotes: 1

Related Questions