RまたはPythonで2つのテーブルをマージすると、それぞれ数万行になります。しかし、私は完璧なマッチにマージすることはできません。私は、あるキーが別のキーの部分文字列である場合を探しています。一致する部分文字列には複数の単語を含めることができます。 以下の私のブルートフォースコードよりも速いソリューションを探しています。完全一致ではなく、部分文字列のテーブルを効率的にマージする
https://stackoverflow.com/users/170352/brandon-bertelsen私が最初に提案したおもちゃのデータに基づいた素敵な答えをくれました。ただし、1語の部分文字列にのみ一致します。 (私はもともとこの要件を明示していませんでした)
これは私がこの状況で使用するコードです。
library(SPARQL)
library(parallel)
library(Hmisc)
library(tidyr)
library(dplyr)
my.endpoint <- "http://sparql.hegroup.org/sparql/"
go.query <- 'select *
where { graph <http://purl.obolibrary.org/obo/merged/GO>
{ ?goid
<http://www.geneontology.org/formats/oboInOwl#hasOBONamespace>
"biological_process"^^<http://www.w3.org/2001/XMLSchema#string> .
?goid rdfs:label ?goterm}}'
go.result <- SPARQL(url = my.endpoint, query = go.query)
go.result.frame <- go.result[[1]]
anat.query <- 'select distinct ?anatterm ?anatid
where { graph <http://purl.obolibrary.org/obo/merged/UBERON>
{ ?anatid <http://www.geneontology.org/formats/oboInOwl#hasDbXref> ?xr .
?anatid rdfs:label ?anatterm}}'
anat.result <- SPARQL(url = my.endpoint, query = anat.query)
anat.result.frame <- anat.result[[1]]
# slow but recognizes multi-word substrings
loop.solution <-
mclapply(
X = sort(anat.result.frame$anatid),
mc.cores = 7,
FUN = function(one.anat.id) {
one.anat.term <-
anat.result.frame$anatterm[anat.result.frame$anatid == one.anat.id]
temp <-
grepl(pattern = paste0('\\b', one.anat.term, '\\b'),
x = go.result.frame$goterm)
temp <- go.result.frame[temp , ]
if (nrow(temp) > 0) {
temp$anatterm <- one.anat.term
temp$anatid <- one.anat.id
return(temp)
}
}
)
loop.solution <- do.call(rbind, loop.solution)
# from Brandon
# fast, but doesn't recognize multi-word matches
sep.gather.soln <-
separate(go.result.frame,
goterm,
letters,
sep = " ",
remove = FALSE) %>%
gather(goid, goterm) %>%
na.omit() %>%
setNames(c("goid", "goterm", "code", "anatterm")) %>%
select(goid, goterm, anatterm) %>%
left_join(anat.result.frame) %>%
na.omit()
セット(用語+」 『+集合における猫のための用語で用語のために猫([辞書に.get(単語、』 '))の単語のためにterm.split()])もしcat) – galaxyan
ありがとう。私は、行が途切れ、インデントがこのコードに入るかどうかを調べることに問題があります。 –
これは1行のコードです – galaxyan