How to bind one variable to all other variables on R

I want to map one variable (e.g. tyrosine) to all other variables (about 200 other metabolites, such as urea, glucose, inosine, etc.) on R, and I'm not sure how to do this. I am new to R.

I recognized the pair function, but this is a pair of each metabolite in the range indicated by the other.

Thanks!

+6
source share
3 answers

Since you mention “metabolites,” I assume your indicator is “concentration,” for example. that you have a matrix, name it data , which has one column for each metabolite and one row for each sample.

So something like this:

 # just generates example - YOU SHOULD PROVIDE THIS!!! data <- data.frame(tyrosine=1:10 + rnorm(10,sd=2), urea =2*1:10 + rnorm(10,sd=2), glucose =30 -2*1:10 +rnorm(10,sd=2), inosine =25 -1:10 + rnorm(10,sd=2)) data tyrosine urea glucose inosine 1 -0.2529076 5.023562 29.83795 26.71736 2 2.3672866 4.779686 27.56427 22.79442 3 1.3287428 4.757519 24.14913 22.77534 4 7.1905616 3.570600 18.02130 20.89239 5 5.6590155 12.249862 21.23965 17.24588 6 4.3590632 11.910133 17.88774 18.17001 7 7.9748581 13.967619 15.68841 17.21142 8 9.4766494 17.887672 11.05850 16.88137 9 10.1515627 19.642442 11.04370 18.20005 10 9.3892232 21.187803 10.83588 16.52635 

To get the correlation coefficients, simply enter:

 cor(data) tyrosine urea glucose inosine tyrosine 1.0000000 0.8087897 -0.9545523 -0.8512938 urea 0.8087897 1.0000000 -0.8577782 -0.8086910 glucose -0.9545523 -0.8577782 1.0000000 0.8608000 inosine -0.8512938 -0.8086910 0.8608000 1.0000000 

To create a scatter matrix, simply enter:

 pairs(data) 

In the future, please provide an example of your data that can be imported into R.

+9
source

In the following example, I simply split the data frame containing all the variables into two matrices. They can be introduced into the cor function to obtain 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 
+6
source

similar to Marc in a field using application 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 

A good question and a template is to know data of a reasonable size, since it is effective if you want only tyrosine cors (what OP specifically asked), only to calculate tyrosine cors (n time + space), not everything is against everyone (~ n ^ 2 times + space).

+4
source

Source: https://habr.com/ru/post/959421/


All Articles