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!
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.
A correlation is usually tested for two variables at a time, but you can test correlations between three or more variables.
Use the function cor. test(x,y) to analyze the correlation coefficient between two variables and to get significance level of the correlation.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With