查找两个列表之间的类似元素,并使用相应的元素替换

时间:2014-04-27 07:22:43

标签: r replace match

我有一个探测ID列表,如下所示:

> dput(best)
list(c("204639_at", "203440_at", "242136_x_at", "231954_at", 
"208388_at", "205942_s_at", "203510_at", "204639_at"), c("204639_at", 
"203510_at", "231954_at")) 

然后我使用了这个文件:

 > head(sym)
                x
204639_at     ADA
203440_at    CDH2
242876_at    AKT3
207078_at    MED6
208388_at   NR2E3
222161_at NAALAD2

> class(sym)
[1] "data.frame"

然后,我想找到其他名称:

("ADA" "CDH2" "AKT3" "MED6" "NR2E3" "NAALAD2")

sym中,将“现有”替换为“最佳”文件中的元素。有人有黑客攻击吗?感谢

1 个答案:

答案 0 :(得分:1)

没有" hack"需要的。

#your data:
best <- list(list(c("204639_at",  "203440_at",   "242136_x_at", "231954_at",   "208388_at", "205942_s_at", "203510_at",   "204639_at" )),
             list(c("204639_at",  "203510_at",   "231954_at")))
sym <- read.table(text="                x
204639_at     ADA
203440_at    CDH2
242876_at    AKT3
207078_at    MED6
208388_at   NR2E3
222161_at NAALAD2", header=TRUE)

#iterate through list and match against sym
rapply(best, function(x) {
  res <- as.character(sym[x,1])
  #omit the following line if you prefer NAs for nomatches
  res[is.na(res)] <- x[is.na(res)]
  res
  }, how="list")

#[[1]]
#[[1]][[1]]
#[1] "ADA"         "CDH2"        "242136_x_at" "231954_at"   "NR2E3"       "205942_s_at" "203510_at"   "ADA"        
#
#
#[[2]]
#[[2]][[1]]
#[1] "ADA"       "203510_at" "231954_at"