用apply替换循环

时间:2018-10-11 14:48:48

标签: r for-loop matrix apply

我有一个简单的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))

1 个答案:

答案 0 :(得分:0)

同意注释,使用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