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 at r-project.org> On Behalf Of Val
Sent: Monday, August 22, 2022 11:09 AM
To: r-help at R-project.org (r-help at r-project.org) <r-help at
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
0.11 1.52 0.73 -1.03",header=TRUE)
#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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Cf0bf7462434f445fdc3808da84505c52%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637967777937186965%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Oqo1ikNvtAix%2Fj7jax%2Bsf53J5eDHB0LHnRSHEy9O5hM%3D&reserved=0
PLEASE do read the posting guide
https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Cf0bf7462434f445fdc3808da84505c52%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637967777937186965%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=TWJ%2BJxRdA2S7PKBnsYg3DiSdFtSxIit6v1HOAi7Hft8%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.