# [R] Correlate

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Mon Aug 22 17:53:48 CEST 2022

```A somewhat clunky solution:
for(i in colnames(dat)){
print(cor.test(dat[,i], dat\$x1, method = "pearson", use = "complete.obs")\$estimate)
print(cor.test(dat[,i], dat\$x1, method = "pearson", use = "complete.obs")\$p.value)
}

Rather than printing you could set up an array or list to save the results.

Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Val
Sent: Monday, August 22, 2022 11:09 AM
To: r-help using R-project.org (r-help using r-project.org) <r-help using r-project.org>
Subject: [R] Correlate

[External Email]

Hi all,

I have a data set with  ~250  variables(columns).  I want to calculate the correlation of  one variable with the rest of the other variables and also want  the p-values  for each correlation.  Please see the sample data and my attempt.  I  have got the correlation but unable to get the p-values

dat <- read.table(text="x1 x2 x3 x4
1.68 -0.96 -1.25  0.61
-0.06  0.41  0.06 -0.96
.    0.08  1.14  1.42
0.80 -0.67  0.53 -0.68
0.23 -0.97 -1.18 -0.78
-1.03  1.11 -0.61    .
2.15     .    0.02  0.66
0.35 -0.37 -0.26  0.39
-0.66  0.89   .    -1.49

#change all to numeric
dat[] <- lapply(dat, function(x) as.numeric(as.character(x)))

data_cor <- cor(dat[ , colnames(dat) != "x1"],  dat\$x1, method = "pearson", use = "complete.obs")

Result
[,1]
x2 -0.5845835
x3 -0.4664220
x4  0.7202837

How do I get the p-values ?

Thank you,

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see