【问题标题】:R: Match dataframes and filter theseR:匹配数据帧并过滤这些
【发布时间】:2011-08-27 19:36:54
【问题描述】:

我有一个像变量 k 这样的数据框。 all_possible_names 列包含更多 ILMN 代码的标识符。 现在我想在 all_possible_names 列中搜索数据框标识符中可用的标识符。

z <- matrix(c(0,0,1,1,0,0,1,1,0,0,0,0,1,0,1,1,0,1,1,1,1,0,0,0,"RND1 | AB849382 | uc001aeu.1","WDR | AB361738 | uc001aif.1", "PLAC8 | AB271612 | uc001amd.1","TYBSA | AB859482","GRA | AB758392 | uc001aph.1","TAF | AB142353"), nrow=6,
dimnames=list(c("ILMN_1651838","ILMN_1652371","ILMN_1652464","ILMN_1652952","ILMN_1653026","ILMN_1653103"),c("A","B","C","D","all_possible_names")))
k<-as.data.frame(z)

search<-c("AB361738","RND1", "LIS")
identifier <- as.data.frame(search)

结果一定是这样的:

    search    Names
1 AB361738    WDR | AB361738 | uc001aif.1
2     RND1    RND1 | AB849382 | uc001aeu.1
3      LIS    NA

创建此数据框后,可以创建最终输出。列名只能包含以 uc0 开头的命名。

最终结果将是:

    search    Names
1 AB361738    uc001aif.1
2     RND1    uc001aeu.1
3      LIS    NA

谁能帮我解决这个问题?

非常感谢, 丽珊娜

【问题讨论】:

    标签: r filter match dataframe


    【解决方案1】:

    可能不是最好的方式,而是一种方式:

    firstStep<-lapply(srch, grep, k$all_possible_names, fixed=TRUE, value=TRUE)
    res<-lapply(firstStep, function(subres){
            prts<-unlist(strsplit(subres, " | ", fixed=TRUE))
            prts[which(substr(prts, 1, 3)=="uc0")]
        })
    

    这会将结果作为列表返回,因为您可能不确定每个搜索字符串只有一个结果。

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 2011-08-23
      • 2021-12-29
      • 1970-01-01
      • 2013-10-18
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      相关资源
      最近更新 更多