Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Correlate one variable to all other variables on R [duplicate]

I want to correlate one variable (say tyrosine) with all the other variables (about 200 other metabolites, like urea, glucose, inosine, etc) on R, and I'm not sure how to go about it. I'm new to R.

I've learned the pair function, but that pairs every metabolite in the range specified to the other.

Thanks!

like image 308
user3071533 Avatar asked Dec 05 '13 21:12

user3071533


People also ask

How do you correlate multiple variables in R?

In this method, the user has to call the cor() function and then within this function the user has to pass the name of the multiple variables in the form of vector as its parameter to get the correlation among multiple variables by specifying multiple column names in the R programming language.

Can you correlate more than 2 variables?

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

How do you correlate data in R?

Use the function cor. test(x,y) to analyze the correlation coefficient between two variables and to get significance level of the correlation.

How do you correlate two variables?

The correlation coefficient is measured on a scale that varies from + 1 through 0 to – 1. Complete correlation between two variables is expressed by either + 1 or -1. When one variable increases as the other increases the correlation is positive; when one decreases as the other increases it is negative.


2 Answers

similar to Marc in the box using the apply and column names

> set.seed(1)
> n=20
> df <- data.frame(tyrosine=runif(n), urea=runif(n), glucose=runif(n), 
  inosine=runif(n))

> apply(df,2, function(col)cor(col, df$tyrosine))

tyrosine       urea    glucose    inosine 
1.0000000 -0.2373854 -0.3672984 -0.3393602 

It's a good question, and pattern to know for data of reasonable size, as it's efficient if you only want tyrosine cors (what the OP specifically asked) to only calculate tyrosine cors (n time + space), not all vs all (~n^2 time + space).

like image 162
Stephen Henderson Avatar answered Nov 09 '22 14:11

Stephen Henderson


In the following example, I simply split a data frame that contains all of the variables into two matrices. These can be entered into the cor function to obtain your correlation values:

set.seed(1)
n=20
df <- data.frame(tyrosine=runif(n), urea=runif(n), glucose=runif(n), inosine=runif(n))
df

COR <- cor(as.matrix(df[,1]), as.matrix(df[,-1]))
COR
#           urea    glucose    inosine
#[1,] -0.2373854 -0.3672984 -0.3393602
like image 39
Marc in the box Avatar answered Nov 09 '22 14:11

Marc in the box