2011-07-20 52 views
4

我想計算一個數據集x的一個子集的柱子之間的相關係數R 我有40個模型的行,每個200個模擬在總共8000行中 我想計算下列兩者之間的係數corr:每個模擬colums(40行)如何計算相關性R

cor(x[c(3,5)])所有8000行
我需要cor(x[c(3,5)])計算,但只有當X$nsimul=1

,你會幫我在這方面 聖

+0

'肺心病(X [X $ nsimul == 1,C (3,5)])?沒有可重複的例子,這個問題很難回答。 –

回答

6

我不知道你與x[c(3,5)]做什麼,但它看起來像你想要做的事這樣的:你有一個數據幀X這樣的:

set.seed(123) 
X <- data.frame(nsimul = rep(1:2, each=5), a = sample(1:10), b = sample(1:10)) 

> X 
    nsimul a b 
1  1 1 6 
2  1 8 2 
3  1 9 1 
4  1 10 4 
5  1 3 9 
6  2 4 8 
7  2 6 5 
8  2 7 7 
9  2 2 10 
10  2 5 3 

而且要將該數據幀拆分爲nsimul列,並計算每個組中的ab之間的相關性。這是一個經典的split-apply-combine問題爲其plyr包是非常非常適合:

require(plyr) 
> ddply(X, .(nsimul), summarize, cor_a_b = cor(a,b)) 
    nsimul cor_a_b 
1  1 -0.7549232 
2  2 -0.5964848 
1

可以使用by的功能,例如:

correlations <- as.list(by(data=x,INDICES=x$nsimul,FUN=function(x) cor(x[3],x[5]))) 

# now you can access to correlation for each simulation 
correlations["simulation 1"] 
correlations["simulation 2"] 
... 
correlations["simulation 40"]