2011-12-19 50 views
1

我有一組數據,如:R - 列子集之間的相關性 - 參考當前行

 name  Exp1Res1 Exp1Res2 Exp1Res3 ExpRes1 Exp2Res2 Exp3Res3 

[1]  ID1   5   7   9   7   9  2 

[2]  ID2   6   4   2   9   5  1 

[3]  ID3   4   9   9   9   11  2 

我需要確定每個行的實驗1和2之間的相關性。由於在我的數據集(FullSet)中實際上有37列和100,000行,我原來的循環解決方案太慢(參見下文),所以我想優化。

我原來的解決方案是;

df <- data.frame(matrix(ncol = 5, nrow = dim(FullSet)[1])) 
names(df)<-c("ID","pearson","spearman") 
for (i in seq(1, dim(FullSet)[1])) 
{ 
    pears=cor(as.numeric(t(FullSet[i,2:19])),as.numeric(t(FullSet[i,20:37])), method="pearson") 
    spear=cor(as.numeric(t(FullSet[i,2:19])),as.numeric(t(FullSet[i,20:37])), method="pearson") 
    df[i,]<-c(FullSet[i,1],pears,spear) 
} 

我覺得應該這樣工作;

FullSet$pearson<-cor(as.numeric(t(FullSet[,2:19])),as.numeric(t(FullSet[,20:37])), method="pearson") 

,但我不知道是否/如何在轉引用只要當前行 -

t(FullSet[,2:19]) - which should read something like t(FullSet[<currow>,2:19]). 

幫助,將不勝感激 - 我不知道我的做法是正確的,甚至。

輸出應該像(結果是不正確的 - 例如只)

 name  Pearson  Spearman 

[1]  ID1   0.8   .75 

[2]  ID2   0.9   .8 

[3]  ID3   0.85   .7 

回答

4

怎麼樣將它帶到格式:

ID EXP Res 
1 1 . 
1 1 . 
1 2 . 
1 2 . 

使用reshape,然後讓plyr做的工作:

require(plyr) 
ddply(df, .(ID, EXP), summarize, cor(...)) 

會這樣嗎?如果你單獨爲spearman和perason做。

+0

我已經將此標記爲答案,因爲它確實有效並且是一種替代方法(只是熔化和ddply),但它不會比循環方法更快。 – statler 2011-12-19 23:05:59