2016-05-03 25 views
0

私は説明するのに徹底的に挑戦します。別のデータフレーム内の一致するデータに応じてデータフレーム内の値を更新します。

2つのデータフレームがあり、重要な列はuID(一意の識別子)とb(遺伝子型を示す値)です。 1つのデータフレームでdelsFB、各uIDが1回だけ表示されます(注:uIDsは必ずしも連続している必要はありません)。他の例では、delsOBでは、異なる行に異なる数値(1〜100回の範囲で)が表示されます。 delsFBuIDsdelsOBのものに合わせて、bの列を新しい値に更新する必要があります。

ワークフロー:delsOBの行ごと

  1. は、同じuIDdelsFBの行を探し。
  2. delsOBの一致する行のいずれかにb値がKP、もう1つがCPの場合は、delsFB$bに値NPを入力します。
  3. KPが含まれている行にCPが含まれていない場合は、KPdelsFB$bに入力します。
  4. CPが含まれていて、いずれも含まれていない行がKPの場合は、delsFB$bに値CPを入力します。
  5. その他の状況でもNPを返す必要があります。

私はそれが意味を成し遂げたことを願っております。ありがとうございました。

DELS $ FB:

structure(list(chrom = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), pos_c = c(28522L, 29000L, 29211L, 29423L, 
29462L, 29552L, 29766L, 29791L, 29885L, 29946L, 29986L, 30157L, 
30587L, 30871L), uID = c(24L, 33L, 44L, 56L, 62L, 68L, 76L, 80L, 
86L, 91L, 92L, 101L, 141L, 161L), pos_k = c(28523L, 29011L, 29221L, 
29427L, 29462L, 29551L, 29766L, 29790L, 29887L, 29946L, 29953L, 
30123L, 30557L, 30841L), seq_c = structure(c(3L, 3L, 2L, 3L, 
3L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 3L), .Label = c("A", "C", 
"G"), class = "factor"), seq_sk = structure(c(3L, 3L, 2L, 3L, 
3L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 3L), .Label = c("A", "C", 
"G"), class = "factor"), type_c = c(complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
complex(real=0, imaginary=NA)), type_k = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "d", class = "factor"), 
    Var_len = c(2L, 3L, 7L, 6L, 3L, 2L, 2L, 2L, 3L, 34L, 2L, 
    2L, 4L, 2L), reads_all = c(20L, 25L, 27L, 18L, 17L, 12L, 
    26L, 26L, 29L, 27L, 16L, 26L, 47L, 42L), deletions = c(0L, 
    0L, 5L, 1L, 15L, 0L, 0L, 0L, 3L, 1L, 2L, 1L, 0L, 1L), insertions = c(0L, 
    1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), A = c(0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 24L, 25L, 1L, 1L, 47L, 0L), C = c(0L, 
    0L, 20L, 1L, 0L, 11L, 25L, 0L, 2L, 1L, 11L, 22L, 0L, 1L), 
    T = c(0L, 1L, 2L, 0L, 2L, 1L, 0L, 0L, 0L, 0L, 2L, 2L, 0L, 
    0L), G = c(20L, 24L, 0L, 16L, 0L, 0L, 0L, 26L, 0L, 0L, 0L, 
    0L, 0L, 40L), k = c(0L, 0L, 5L, 1L, 15L, 0L, 0L, 0L, 3L, 
    1L, 2L, 1L, 0L, 1L), c = c(20L, 24L, 20L, 16L, 0L, 11L, 25L, 
    26L, 24L, 25L, 11L, 22L, 47L, 40L), SNPreads = c(20L, 24L, 
    25L, 17L, 15L, 11L, 25L, 26L, 27L, 26L, 13L, 23L, 47L, 41L 
    ), b = structure(c(1L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, NA, 1L, 
    3L, 1L, 1L, 1L), .Label = c("CP", "KP", "NP"), class = "factor")), .Names = c("chrom", 
"pos_c", "uID", "pos_k", "seq_c", "seq_sk", "type_c", "type_k", 
"Var_len", "reads_all", "deletions", "insertions", "A", "C", 
"T", "G", "k", "c", "SNPreads", "b"), class = "data.frame", row.names = c(NA, 
-14L)) 

delsOB:

structure(list(chrom = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), pos_c = c(28523L, 29001L, 29002L, 29212L, 29213L, 
29214L, 29215L, 29216L, 29217L, 29424L, 29425L, 29426L, 29427L, 
29428L, 29463L, 29464L, 29553L, 29767L, 29792L, 29886L, 29887L, 
29947L, 29948L, 29949L, 29950L, 29951L, 29952L, 29953L, 29954L, 
29955L, 29956L, 29957L, 29958L, 29959L, 29960L, 29961L, 29962L, 
29963L, 29964L, 29965L, 29966L, 29967L, 29968L, 29969L, 29970L, 
29971L, 29972L, 29973L, 29974L, 29975L, 29976L, 29977L, 29978L, 
29979L, 29987L, 30158L, 30588L, 30589L, 30590L, 30872L), uID = c(24L, 
33L, 33L, 44L, 44L, 44L, 44L, 44L, 44L, 56L, 56L, 56L, 56L, 56L, 
62L, 62L, 68L, 76L, 80L, 86L, 86L, 91L, 91L, 91L, 91L, 91L, 91L, 
91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 
91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 91L, 
91L, 92L, 101L, 141L, 141L, 141L, 161L), pos_k = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "-", class = "factor"), 
    seq_c = structure(c(1L, 4L, 1L, 1L, 4L, 3L, 3L, 4L, 4L, 1L, 
    2L, 1L, 2L, 4L, 1L, 1L, 3L, 4L, 4L, 4L, 4L, 2L, 1L, 3L, 3L, 
    3L, 2L, 1L, 4L, 3L, 1L, 3L, 4L, 4L, 1L, 2L, 3L, 4L, 2L, 1L, 
    1L, 4L, 2L, 4L, 2L, 4L, 3L, 3L, 4L, 2L, 1L, 1L, 4L, 2L, 1L, 
    1L, 4L, 2L, 3L, 1L), .Label = c("A", "C", "G", "T"), class = "factor"), 
    seq_sk = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = "-", class = "factor"), 
    type_c = c(complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA), 
    complex(real=0, imaginary=NA), complex(real=0, imaginary=NA) 
    ), type_k = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = "d", class = "factor"), 
    Var_len = c(2L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 
    6L, 6L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 34L, 34L, 34L, 34L, 34L, 
    34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 
    34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 
    34L, 34L, 34L, 34L, 2L, 2L, 4L, 4L, 4L, 2L), reads_all = c(20L, 
    24L, 21L, 27L, 27L, 27L, 27L, 28L, 28L, 16L, 16L, 16L, 16L, 
    16L, 17L, 17L, 12L, 26L, 26L, 29L, 29L, 27L, 27L, 19L, 20L, 
    17L, 17L, 15L, 15L, 15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 
    14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
    14L, 14L, 14L, 14L, 14L, 16L, 26L, 47L, 45L, 45L, 42L), deletions = c(18L, 
    0L, 1L, 25L, 24L, 24L, 21L, 20L, 20L, 13L, 13L, 13L, 13L, 
    12L, 0L, 0L, 11L, 21L, 25L, 23L, 22L, 7L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 8L, 8L, 9L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 9L, 8L, 10L, 
    20L, 44L, 44L, 44L, 38L), insertions = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), A = c(2L, 0L, 
    0L, 0L, 1L, 1L, 1L, 2L,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13L, 12L, 0L, 0L, 
    11L, 21L, 25L, 23L, 22L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 8L, 8L, 9L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 9L, 8L, 10L, 20L, 44L, 44L, 
    44L, 38L), c = c(2L, 24L, 0L, 0L, 2L, 0L, 1L, 6L, 5L, 1L, 
    2L, 2L, 1L, 3L, 0L, 0L, 0L, 2L, 0L, 2L, 3L, 16L, 16L, 2L, 
    9L, 1L, 8L, 3L, 1L, 0L, 4L, 2L, 3L, 3L, 4L, 3L, 0L, 1L, 2L, 
    1L, 0L, 2L, 3L, 3L, 1L, 4L, 1L, 4L, 5L, 1L, 4L, 3L, 3L, 1L, 
    3L, 4L, 2L, 0L, 0L, 3L), SNPreads = c(20L, 24L, 1L, 25L, 
    26L, 24L, 22L, 26L, 25L, 14L, 15L, 15L, 14L, 15L, 0L, 0L, 
    11L, 23L, 25L, 25L, 25L, 23L, 25L, 11L, 18L, 10L, 17L, 12L, 
    10L, 9L, 12L, 10L, 12L, 11L, 12L, 11L, 9L, 10L, 11L, 10L, 
    9L, 11L, 12L, 12L, 10L, 13L, 10L, 12L, 13L, 9L, 12L, 11L, 
    12L, 9L, 13L, 24L, 46L, 44L, 44L, 41L), b = structure(c(3L, 
    1L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
    3L, 3L, 3L, 3L, 3L, 4L, 2L, 4L, 4L, 3L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
    3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("CP", 
    "HP", "KP", "NP"), class = "factor")), .Names = c("chrom", 
"pos_c", "uID", "pos_k", "seq_c", "seq_sk", "type_c", "type_k", 
"Var_len", "reads_all", "deletions", "insertions", "A", "C", 
"T", "G", "k", "c", "SNPreads", "b"), class = "data.frame", row.names = c(NA, 
-60L)) 

EDIT: - これらの例のデータセットのため、b列申し訳ありませんが、忘れてしまった予想出力ここdputで作られた2つの小さな例のデータフレームは、 delsFBは、 24 KP、33 CP、44 KP、56 KP、62 NP、68 KP、76 KP、80 KP、86 KP、91 KP、92 KP、101 KP、141 KP、161 KPに更新する必要があります。

現在、 uID91delsOBに34のインスタンスがありますが、それでもKPと入力すると、KPと見なされます。理想的には、ルールはある程度長さに依存する。 34のインスタンスがある場合は、そのうちの少なくとも50%がb=KPの場合はKPである必要がありますが、〜5インスタンスしかない場合はそのインスタンスの80%にする必要があります。しかし、これは問題を複雑にする可能性があります。

+0

ワークフローで手順2を説明できますか? – user2007598

答えて

1

これにアプローチする方法の1つは、delsOBの一意の値をループして、希望する条件を確認し、delsFBを更新することです。次のコードでこれを行う必要があり、追加の条件(インスタンス数に基づいて結果を変更するなど)を含めるように変更できます。

# Save delsFB to a new data frame so results can be compared afterward. 
delsFB_new <- delsFB 

# Loop through the unique uID values in delsOB. 
for (unique_id in unique(delsOB$uID)) { 
    # Subset delsOB for the current unique uID. 
    currrent_delsOB_subset <- delsOB[delsOB$uID == unique_id, ] 
    # Assign desired value to result based on the conditions. 
    if ("KP" %in% currrent_delsOB_subset$b & "CP" %in% currrent_delsOB_subset$b) { 
    result <- "NP" 
    } 
    else if ("KP" %in% currrent_delsOB_subset$b) { 
    result <- "KP" 
    } 
    else if ("CP" %in% currrent_delsOB_subset$b) { 
    result <- "CP" 
    } 
    else { 
    result <- "NP" 
    } 
    # Insert the result into delsFB$b. 
    delsFB_new[delsFB_new$uID == unique_id, ]$b <- result 
} 

EDIT:ここでインスタンスの数に基づいて、「KP」の割合のための追加のチェックのための条件付き論理を使用する第2のバージョンがあります。

# Save delsFB to a new data frame so results can be compared afterward. 
delsFB_new <- delsFB 

# Loop through the unique uID values in delsOB. 
for (unique_id in unique(delsOB$uID)) { 
    # Subset delsOB for the current unique uID. 
    currrent_delsOB_subset <- delsOB[delsOB$uID == unique_id, ] 
    # Assign desired value to result based on the conditions. 
    if ("KP" %in% currrent_delsOB_subset$b & "CP" %in% currrent_delsOB_subset$b) { 
    result <- "NP" 
    } 
    else if ("KP" %in% currrent_delsOB_subset$b) { 
    # Nested conditional logic to ensure a minimum proportion of "KP" values, according to the number of instances. 
    if (length(currrent_delsOB_subset$b) <= 7 & length(currrent_delsOB_subset$b[currrent_delsOB_subset$b == "KP"]) >= .8*length(currrent_delsOB_subset$b)) { 
     result <- "KP" 
    } 
    else if (length(currrent_delsOB_subset$b) > 7 & length(currrent_delsOB_subset$b[currrent_delsOB_subset$b == "KP"]) >= .5*length(currrent_delsOB_subset$b)) { 
     result <- "KP" 
    } 
    } 
    else if ("CP" %in% currrent_delsOB_subset$b) { 
    result <- "CP" 
    } 
    else { 
    result <- "NP" 
    } 
    # Insert the result into delsFB$b. 
    delsFB_new[delsFB_new$uID == unique_id, ]$b <- result 
} 
+0

余分な基準を満たすように修正することは可能でしょうか?理想的には、ルールはある程度長さに依存する。 34のインスタンスがある場合、それらのうち少なくとも50%はb = KPに対してKPでなければならないが、インスタンスが〜5つしかない場合、そのインスタンスの80%でなければならない。しかしこれは必須ではありません。あなたの助けをありがとう! – Margins

+1

うれしかった!現在のインスタンス数に基づいて、最小の「KP」比率のしきい値が満たされていることを確認するために、追加のチェックを追加するように思えます。ロジックを正確に把握したら、ネストされた条件付きロジックでこれを実行できます。私はあなたのためにコードの2番目のバージョンを追加しました。 "#Nested Conditional Logic ..."で始まるコメントを探し、必要なロジックに基づいて続くコードを修正してください。 – Ravi

+0

もう一度、ありがとうございます。 – Margins

関連する問題