2013-12-18 35 views
2

所以我堅持看起來是基本的東西,但無論如何。我有下面的數據框(Dat.f),我需要在Posgain.vector和Neggain.vector上運行比例.test(Chi.squared),並使用公式prop.test提取P值 。使用數據框的列中的值進行比例測試

如果使用第一行作爲一個例子,這將變成是

P<-prop.test(x=c(4,4), n=c(16,10)[2] 

    Posgain.vector Freq Neggain.vector Freq.1 PosRef NegRef 
1   A1BG 4   A1BG  4  16  10 
2  A1BG-AS1 4  A1BG-AS1  4  16  10 
3   A1CF 4   A1CF  1  16  10 
4   A2M 1   A2M  1  16  10 
5  A2M-AS1 1  A2M-AS1  1  16  10 
6   A2ML1 1   A2ML1  1  16  10 

手工完成的麻煩的是我不能完全弄清楚如何將這個功能,因此它在貼適用於每一行將該行的第二和第四列轉換爲x並應用n保持不變的公式。

任何幫助將不勝感激。

回答

2

非常相似的方法來@agstudy,但提取p.value:

df = data.frame(Posgain.vector=c("A1BG", "A1BG-AS1", "A1CF", "A2M", "A2M-AS1", "A2ML1"), 
       Freq = c(4, 4, 4, 1, 1, 1), 
       Neggain.vector=c("A1BG", "A1BG-AS1", "A1CF", "A2M", "A2M-AS1", "A2ML1"), 
       Freq.1 = c(4, 4, 1, 1, 1, 1), 
       PosRef = rep(16, 6), 
       NegRef = rep(10, 6)) 
apply(df[, c(2,4)], 1, function(row) prop.test(x=c(row[1], row[2]), n=c(16, 10))$p.value) 

# [1] 0.7117401 0.7117401 0.6652053 1.0000000 1.0000000 1.0000000 
+0

非常感謝您! –

相關問題