Reputation: 4623
I have a dataset like this
set.seed(1)
a = abs(rnorm(10, mean = 0, sd= 1))
b = abs(rnorm(10, mean = 0, sd= 1))
c = abs(rnorm(10, mean = 0, sd= 1))
d = abs(rnorm(10, mean = 0, sd= 1))
df = as.data.frame(cbind(a, b, c, d))
And i want to get a table
c d
a 0.5 0.1
b 0.8 0.3
where cols and rows are the variables and cells - correlation coefficients between variables.
I do as below
for(j in df[, 1:2]) {
for(i in df[, 3:4]) {
k=abs(cor.test(j, i, method = c( "spearman"))$estimate)
cat(k, '\n')
y <- rbind(y, k)
}}
y
and get
rho
k 0.175757576
k 0.006060606
k 0.151515152
k 0.054545455
I used this post Using double loop to fill a matrix in R
mat<-matrix(list(c(NA,NA)), nrow=2, ncol=2)
for(j in df[, 1:2]) {
for(i in df[, 3:4]) {
mat[i,j][[1]]=abs(cor.test(j, i, method = c( "spearman"))$estimate)
}}
mat
and i get
[,1] [,2]
[1,] Logical,2 Logical,2
[2,] Logical,2 Logical,2
how to fill the table? Or can i fill it without loop?
ggpairs
Upvotes: 3
Views: 1075
Reputation: 12559
The function cor()
can do that:
set.seed(1)
a = abs(rnorm(10, mean = 0, sd= 1))
b = abs(rnorm(10, mean = 0, sd= 1))
c = abs(rnorm(10, mean = 0, sd= 1))
d = abs(rnorm(10, mean = 0, sd= 1))
#### df = as.data.frame(cbind(a, b, c, d)) # not used
cor(cbind(a,b), cbind(c,d))
# > cor(cbind(a,b), cbind(c,d))
# c d
# a 0.5516642 -0.3918783
# b 0.8200195 0.1474773
And you can do abs()
for your desired result:
abs(cor(cbind(a,b), cbind(c,d)))
# > abs(cor(cbind(a,b), cbind(c,d)))
# c d
# a 0.5516642 0.3918783
# b 0.8200195 0.1474773
Spearman:
abs(cor(cbind(a,b), cbind(c,d), method = "spearman"))
# > abs(cor(cbind(a,b), cbind(c,d), method = "spearman"))
# c d
# a 0.05454545 0.40606061
# b 0.75757576 0.05454545
If you want to use your dataframe, you can do:
df = as.data.frame(cbind(a, b, c, d))
rm(a,b,c,d) ### to be sure that a, ..., d are from the dataframe.
with(df, abs(cor(cbind(a,b), cbind(c,d), method = "spearman")))
or
abs(cor(df[,c("a", "b")], df[,c("c","d")], method = "spearman"))
Upvotes: 1
Reputation: 32538
I would compute correlation matrix for df
one time and then subset whatever combinations I need from that. This way, you wouldn't have to run cor
multiple times.
m = cor(df, method = "spearman")
m[row.names(m) %in% c("a","b"), colnames(m) %in% c("c","d")]
# c d
#a 0.05454545 -0.40606061
#b 0.75757576 0.05454545
Upvotes: 2