我正在try 创建一个新的数据库,其中包含按组(三个分组变量)的相关性.目前的问题是,结果总是包含一个相同的值(而不是不同的组值).
我们try 了以下代码:
library(dplyr)
calc_cor <- function(df) {
list(
Cor1x2 = cor(df$var1, df$var2, method = "spearman", use = "pairwise.complete.obs"),
Cor1x3 = cor(df$var1, df$var3, method = "spearman", use = "pairwise.complete.obs"),
Cor1x4 = cor(df$var1, df$var4, method = "spearman", use = "pairwise.complete.obs"),
Cor1x5 = cor(df$var1, df$var5, method = "spearman", use = "pairwise.complete.obs"),
Cor2x3 = cor(df$var2, df$var3, method = "spearman", use = "pairwise.complete.obs"),
Cor2x4 = cor(df$var2, df$var4, method = "spearman", use = "pairwise.complete.obs"),
Cor2x5 = cor(df$var2, df$var5, method = "spearman", use = "pairwise.complete.obs"),
Cor3x4 = cor(df$var3, df$var4, method = "spearman", use = "pairwise.complete.obs"),
Cor3x5 = cor(df$var3, df$var5, method = "spearman", use = "pairwise.complete.obs"),
Cor4x5 = cor(df$var4, df$var5, method = "spearman", use = "pairwise.complete.obs")
)
}
# „group“ variable contains values from 1 to 55
# „meeting“ variable contains values from 1 to 6
# „session_phase“ variable contains values from 1 to 2.
result <- df %>%
group_by(group, meeting, session_phase) %>%
summarise(correlations = list(calc_cor(.))) %>%
unnest_wider(correlations) %>%
as.data.frame()
每一列,特别是var4和var5,包含多个NA,它们应该被忽略,以便框架包含可以在最后使用的值的相关性.
输入数据数据
group meeting session_phase min var1 var2 var3 var4 var5
1 1 1 0 0,3 0,19 0,26
1 1 1 1 0,46 0,28 0,15
1 1 1 2 0,42 0,39 0,22
2 1 1 0 0,65 0,52 1,26
2 1 1 1 0,94 0,36 1,22
2 1 1 2 0,64 0,43 1,31
2 1 1 3 0,55 0,32 1 0,95
…
1 1 2 0 0,55 0,82 0,79 0,95
1 1 2 1 1,02 1,02 1,09 0,7
1 1 2 2 0,69 0,71 0,95 0,54
2 1 2 0 0,59 0,31 0,7 0,37
2 1 2 1 0,34 0,2 0,54 0,59
2 1 2 2 0 0,55 0,2 0,37
…
55 6 2 0 0,81 0 1,2 0,58
55 6 2 1 0,18 1,2 0,58
55 6 2 2 0,27 1,14 0,39
var1到var5现在应该彼此相关(var1与var2、var1与var3、var1与var4等), 通过分组变量group、meeting和session—phase,所以我们最终得到了一个从var1到var2的相关性,用于组1、meeting 1、session 1,等等,从var1到var2的相关性.
虚假输出
group meeting session_phase Cor1x2 Cor1x3 Cor1x4 Cor1x5 Cor2x3 Cor2x4 Cor2x5 Cor3x4 Cor3x5 Cor4x5
1 1 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 1 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 2 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 2 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 3 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 3 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 4 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 4 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 5 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 5 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 6 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
1 6 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
2 1 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
2 1 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
2 2 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
2 2 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
2 3 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
2 3 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
…
55 6 1 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
55 6 2 0,333523752 0,32578531 0,399857124 0,494264624 0,331031329 0,315864637 0,697428913 0,407285621 0,834623448 0,622817882
Cor1x2—Cor4x5的预期输出值应为每行和列不同.我想我忘记了一个步骤,以便把所有的东西都正确地联系起来.我该怎么解决这个问题?