【问题标题】:Replacing for loop with apply用 apply 替换 for 循环
【发布时间】:2019-03-16 17:03:30
【问题描述】:

我有一个简单的 for 循环,用于模式匹配并从另一个矩阵中获取值。运行大量行有点慢。我正在尝试将其转换为函数,然后使用 apply。但我没有得到与 for 循环相同的结果。有人可以告诉我我做错了什么。谢谢

这是for循环:

exp_target_com = structure(list(X06...2239_normal = c(12.2528814946075,  8.25298920937508), X06...2239_tumor = c(12.476021286337, 6.08504757235585), Ensembl_Id = structure(c(NA_integer_, 
NA_integer_), .Label = "", class = "factor"), HGNC = structure(c(NA_integer_, 
NA_integer_), .Label = "", class = "factor")), .Names = c("X06...2239_normal", "X06...2239_tumor", "Ensembl_Id", "HGNC"), class = "data.frame", row.names = c("A_23_P117082", "A_33_P3246448"))

head(exp_target_com)
#>               X06...2239_normal X06...2239_tumor Ensembl_Id HGNC
#> A_23_P117082          12.252881        12.476021       <NA> <NA>
#> A_33_P3246448          8.252989         6.085048       <NA> <NA>


probe_anno = structure(c("A_23_P117082", "A_33_P3246448", "NM_015987", "NM_080671", "NM_015987", "NM_080671", "ENSG00000013583", "ENSG00000152049", 
"HEBP1", "KCNE4"), .Dim = c(2L, 5L), .Dimnames = list(c("44693", 
"31857"), c("Probe.ID", "SystematicName", "refseq_biomart", "Ensembl_Id", 
"HGNC")))

probe_anno
#>            Probe.ID SystematicName refseq_biomart      Ensembl_Id  HGNC
#> 44693  A_23_P117082      NM_015987      NM_015987 ENSG00000013583 HEBP1
#> 31857 A_33_P3246448      NM_080671      NM_080671 ENSG00000152049 KCNE4



for(i in 1:nrow(exp_target_com)) {
  pos <- which(as.character(probe_anno$Probe.ID) == rownames(exp_target_com)[i])
  if(length(pos) > 0) {
    exp_target_com[i,3] <- as.character(probe_anno$Ensembl_Id)[pos[1]]
    exp_target_com[i,4] <- as.character(probe_anno$HGNC)[pos[1]]
    }
 }

这里是函数和应用

get_anno <- function(data_row, probe_anno) {
  pos <- which(as.character(probe_anno$Probe.ID) == rownames(data_row))
  if (length(pos) > 0) {
    data_row$Ensembl_Id <- as.character(probe_anno$Ensembl_Id)[pos[1]]
    data_row$HGNC <- as.character(probe_anno$HGNC)[pos[1]]
  }
  return(data_row) 
}

apply(exp_target_com, c(1,2), FUN = function(x) get_anno(x, probe_anno))

【问题讨论】:

  • 如果您可以提供minimal reproducible example,这将有助于确定问题。
  • 除非您提供我们可以用来测试您的代码和我们的解决方案的示例数据集,否则这将很难为您提供帮助。
  • 除了需要提供 mre 之外,一个常见的误解是 apply 应该比 for 循环更快。
  • 好的,我已经获取了一个子集并给出了 dput 输出,可以吗?
  • 您是否要逐行遍历 data.frame?如果是这样,您希望将applyMARGIN= 参数设置为1。您已将其设置为 c(1,2),将其应用于表格中的每个 单元格

标签: r for-loop matrix apply


【解决方案1】:

同意 cmets,这看起来使用像 mergedplyr 等效连接函数这样的内置函数会更简单、更快捷。在这里,我将行名转换为列并使用它与probe_anno 连接。

library(dplyr)
exp_target_com2 <- exp_target_com %>% 
  select(-3, -4) %>%
  tibble::rownames_to_column("Probe.ID") %>%
  left_join(probe_anno %>% as.data.frame(), by = ("Probe.ID"))



> exp_target_com2
       Probe.ID X06...2239_normal X06...2239_tumor SystematicName refseq_biomart      Ensembl_Id  HGNC
1  A_23_P117082         12.252881        12.476021      NM_015987      NM_015987 ENSG00000013583 HEBP1
2 A_33_P3246448          8.252989         6.085048      NM_080671      NM_080671 ENSG00000152049 KCNE4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多