0

単純なループ(以下)で2つの異なるデータセットの変数にアクセスする必要があります。(これには、負のベクトルと正のベクトルが同じ長さである必要があることを理解しています...幸いなことに、これは常に当てはまります。)

Groups<-c("bdiControl","bdi")
Positive<-c("PA","Sad_PA","Amuse_PA","Happy_PA","Disgust_PA")
Negative<-c("NA","Sad_NA","Amuse_NA","Happy_NA","Disgust_NA")

for (g in Groups) {
    for (i in Positive) { 

if (sd(Groups[[g]]$Positive[[i]])<sdthresh | sd(Groups[[g]]$Negative[[i]]])<sdthresh){
cat('Standard deviation too low to run\ ',Positive[[i]],Negative[[i]],'\ comparison')
}
else{
corr<-cor(Groups[[g]]$Positive[[i]],Groups[[g]]$Negative[[i]],use="complete.obs") 
print("The correlation between " Positive[[i]] " and " Negative[[i]] " was " corr "for " Groups[[g]])
}
}
}

私が試した他の参照には、g $ i、Groups [g] $ Positive [i]、g $ Positive [[i]]、および同様の順列が含まれます。私は問題解決の轍で自分の車輪を回していると思います。ヘルプ?!:)

4

1 に答える 1

1

このコードには多くの問題がありました。コードが何をしようとしているのかは完全には明確ではありませんでしたが(より明確に質問する必要があります)、これであなたが望むことを実行できると思います。

for (group.name in Groups) {
    g <- get(group.name)  # retrieve the actual data
    for (i in 1:length(Positive)) { 
        if (sd(g[[Positive[i]]]) < sdthresh | sd(g[[Negative[i]]]) < sdthresh) {
               cat('Standard deviation too low to run\ ',
                    Positive[[i]], Negative[[i]], '\ comparison')
        }
        else{
            corr<-cor(g[[Positive[i]]], g[[Negative[i]]],use="complete.obs")
            print(paste("The correlation between", Positive[[i]],
                    "and", Negative[[i]], "was", corr, "in", group.name))
        }
    }
}

たとえば、ランダムなデータセットを作成したとき(常に再現可能な例を提供してください!)、次のようになります。

set.seed(1)
bdicontrol = as.data.frame(matrix(rnorm(100), nrow=10))
bdi = as.data.frame(matrix(rnorm(100), nrow=10))
colnames(bdicontrol) <- c(Positive, Negative)
colnames(bdi) <- c(Positive, Negative)

出力は次のとおりです。

[1] "The correlation between PA and NA was -0.613362711250911 in bdicontrol"
[1] "The correlation between Sad_PA and Sad_NA was 0.321335485805636 in bdicontrol"
[1] "The correlation between Amuse_PA and Amuse_NA was 0.0824438791207575 in bdicontrol"
[1] "The correlation between Happy_PA and Happy_NA was -0.192023690189678 in bdicontrol"
[1] "The correlation between Disgust_PA and Disgust_NA was -0.326390681138363 in bdicontrol"
[1] "The correlation between PA and NA was 0.279863504447769 in bdi"
[1] "The correlation between Sad_PA and Sad_NA was 0.115897422274498 in bdi"
[1] "The correlation between Amuse_PA and Amuse_NA was -0.465274556165398 in bdi"
[1] "The correlation between Happy_PA and Happy_NA was 0.268076939911701 in bdi"
[1] "The correlation between Disgust_PA and Disgust_NA was 0.573745174454954 in bdi"
于 2012-08-31T17:48:41.550 に答える