2017-01-25 44 views
2

果実の挙動に対する一対の遺伝子相互作用(> 300K)の影響を分析しており、問題にぶつかっています。私は177本の線を横切って特定の対の遺伝子状態(対立遺伝子)がどれくらいの頻度で出現するかを数えたいと思う。テーブル内の条件付き集計R

いくつかのプレイテーブル:

state1 <-c("A","B","C","A","B","A") 
state2 <- c("B","C","D","D","D","C") 

df1 <- data.frame(state1,state2) 

state <- c("A","B","C","D") 
line_1 <- c(0,0,2,0) 
line_2 <- c(0,0,0,2) 
line_3 <- c(2,2,2,0) 
line_4 <- c(0,2,2,2) 
line_5 <- c(2,0,0,0) 

df2 <- data.frame(state,line_1,line_2,line_3,line_4,line_5, stringsAsFactors = F) 

私は、各状態の組み合わせで行数を返す出力(STATE1 = 0とSTATE2 = 0、STATE1 = 0とSTATE2 = 2、STATE1を取得するために期待しています=これらの組み合わせのそれぞれで2及びSTATE2 = 0、STATE1 = 2及びSTATE2 = 2)とライン:

> resultdf 
    state1 state2 state10state20 state10state22 state12state20 state12state22  lines00  lines02  lines20  lines22 
1  A  B    1    1    2    1  line_1,line_2 line_4  line_5  line_3 
2  B  C    1    2    0    2  line_5 line_1,line_2   NA line_3,line_4 
3  C  D    1    0    2    2  line_5   NA line_1,line_3 line_2,line_4 
4  A  D    1    1    2    1  line_1  line_4 line_3,line_5  line_2 
5  B  D    2    1    1    1 line_1,line_5  line_2  line_3  line_4 
6  A  C    0    2    1    2   NA line_1,line_4  line_5 line_2,line_3 

私はforループやif文の中に見て始めたが、Rが良い他の事をすることがわかりました。私はRの初心者です(そして一般的にコーディングしています)ので、次にどこに向かうのかは分かりませんでした。ご協力いただきありがとうございます。

+0

は、ないでしょう最初の行の 'line_2'は' lines00'の下に表示されますか? – Phil

+0

はい、私はこのエラーについてお詫び申し上げます。私は 'resultsdf'を手作業で集計し、それを誤ってスコアリングしました。正しい 'resultsdf'を反映させるために元の投稿を変更します – mdmartin7

答えて

0

これはそれを行う必要があります。

は、まず、すべての遺伝子の組み合わせのテーブルを作成することができます。 (すでにDF1でこれを持っているが、我々はこれではなく、ハードコードのように、それを自動化することができます):状態

both_true = function(x) x[1,] & x[2,] 
neither_true = function(x) !x[1,] & !x[2,] 
first_true = function(x) x[1,] & !x[2,] 
second_true = function(x) !x[1,] & x[2,] 

別のヘルパーの各可能な組み合わせをテスト

com = combn(unique(df2$state), 2) 

いくつかのヘルパー関数を今、私たちはあなたのデータにこれらの関数を適用することができます

all_pos = function(x) c(
    sum(both_true(x)), 
    sum(neither_true(x)), 
    sum(first_true(x)), 
    sum(second_true(x))) 

を以前のものをそれぞれ使用し、一緒にその結果のカウントをステッチ機能は、

0123を設定します
res = apply(com, 2, function(x) all_pos(df2[df2[,1] %in% x, -1])) 

そして、我々は

同様
colnames(res) = apply(com, 2, paste0, collapse='') 
rownames(res) = c('both', 'neither', 'first', 'second') 

res 
#   AB AC AD BC BD CD 
# both  1 1 0 2 1 1 
# neither 2 1 1 2 2 1 
# first 1 1 2 0 1 2 
# second 1 2 2 1 1 1 

行番号自体を取得するために出力を解釈できるように、列/行名を設定します。 `resultdf``アンダー

which_pos = function(x) c(
    paste(which(both_true(x)), collapse=','), 
    paste(which(neither_true(x)), collapse=','), 
    paste(which(first_true(x)), collapse=','), 
    paste(which(second_true(x)), collapse=',')) 
res2 = apply(com, 2, function(x) which_pos(df2[df2[,1] %in% x, -1])) 
colnames(res2) = apply(com, 2, paste0, collapse='') 
rownames(res2) = c('both', 'neither', 'first', 'second') 
res2 
# 
#   AB AC AD BC BD CD 
# both "3" "3" "" "3,4" "4" "4" 
# neither "1,2" "2" "1" "2,5" "1,5" "5" 
# first "5" "5" "3,5" "" "3" "1,3" 
# second "4" "1,4" "2,4" "1" "2" "2" 
+0

ありがとうございました!与えられたコードを実行すると、すべての行を 'both'または' both'のどちらかに置く出力テーブルが得られます。私は '' AB "" AC "" AD "'の代わりに '' 12 '' 13 "' 14の形式の '' colnames''も取得します。これは私が '1'と' 2'ではなく '0'と' 2'を状態として使用していることが原因でしょうか? – mdmartin7

+0

私はテストしました。あなたの問題は、データフレームに文字ではなく要因があることから確認できます。代わりに 'df2 < - data.frame(state、line_1、line_2、line_3、line_4、line_5、stringsAsFactors = F)'を使用してください。 – dww

+0

素晴らしい作品です!今、私の実際のデータセットまでこれを構築してください。ありがとうございました! – mdmartin7