根据另一个数据帧中的匹配数据更新数据框中的值

时间:2016-05-03 15:30:57

标签: r

我有一个棘手的问题,我会尽力解释。

我有两个数据框,重要的列是b(唯一标识符)和delsFB(表示基因型的值)。 在一个数据框uID中,每个uIDs只出现一次(注意:delsOB不一定是连续的)。另一方面,uID,不同的uIDs在不同的行上可以出现可变次数(范围从1到大约100次出现)。 我需要将delsFB中的delsOBb中的delsOB进行匹配,并使用新值更新delsFB列。

工作流程:

  1. 对于uID的每一行,请在delsOB中查找具有相同KP的行。
  2. 如果CP中的任何匹配行的b值为NP而另一个包含delsFB$b,请在KP中输入值CP
  3. 否则,如果任何行包含KP但不包含delsFB$b,请在CP中输入值KP
  4. 如果任何行包含CP但不包含delsFB$b,请在NP中输入值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))
  5. 任何其他情况也应该返回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, 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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))
  6. 我希望这是有道理的,非常感谢任何帮助。以下是使用dput创建的两个小示例数据框:

    德尔斯$ FB:

    b

    delsOB:

    delsFB

    编辑:抱歉,忘了预期输出 - 对于这些示例数据集,uID中的91列应更新为: 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。

    请注意,目前,例如delsOB KPKP中有34个实例,但它仍然只需要一个KP被视为类型b=KP。理想情况下,规则在某种程度上取决于长度如果有34个实例,则{{1}}中至少有50%必须为{{1}},但如果只有~5个实例,则必须为80%。然而,这可能会使问题变得太复杂。

1 个答案:

答案 0 :(得分: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
}

编辑:这是第二个版本,使用条件逻辑进行附加检查&#34; KP&#34;比例基于实例数。

# 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
}