ここでは、reshape2
パッケージからmelt()
へのデータフレームを長い形式にして、各値に独自の行があるようにするソリューションがあります。元のワイドフォームデータは、6つの遺伝子ごとに1行につき60個の値を持ちますが、溶けたロングフォームのデータフレームは360個の値を持ちます。次に、summarize()
をdplyr
から簡単に使用して、ループのない相関を計算することができます。
library(reshape2)
library(dplyr)
names1 <- names(example_data)[4:33]
names2 <- names(example_data)[34:63]
example_data_longform <- melt(example_data, id.vars = c('Gene','clusterFR','clusterHR'))
example_data_longform %>%
group_by(Gene, clusterFR, clusterHR) %>%
summarize(pearsoncor = cor(x = value[variable %in% names1],
y = value[variable %in% names2]))
またdo()
を使用して、Eudaldの答えのように、より詳細な結果を生成することがありました:
detailed_r <- example_data_longform %>%
group_by(Gene, clusterFR, clusterHR) %>%
do(cor = cor.test(x = .$value[.$variable %in% names1],
y = .$value[.$variable %in% names2]))
これはcor
列には、各遺伝子のcor.test()
の結果にリストされてtibbleを出力します。 lapply()
を使用して、リストから出力を抽出することができます。
lapply(detailed_r$cor, function(x) c(x$estimate, x$p.value))
正しく使用していないため動作しません。 'diag(cor(t(example_data [columnnames]))、t(example_data [columnnames])))'または '' purr''でmap2_dbl(as.data.frame(t(example_data [列名]))、as.data .frame(t(example_data [columnnames]))、cor) ' – akrun
あなたの質問を見直し、https://stats.stackexchange.comで回答を得ることをお勧めします。私はあなたのために1つをコーディングするよりも統計的な問題だと思います。 –