0

我想将一个数据框中的一系列拆分文本搜索到另一个数据框中,如果找到,则为它们分配分数。

a= c("inter","cde",'c','d','e', NA)
b= c("travel","dfgh",'d','f','g', 'h',NA)
c= as.data.frame(rbind(a,b))

我们有数据框 c,如上所述,我们有 3 行。我有另一个数据框,其中包含如下

  e= c("cdes")
  f= c("dfgk")
  l=c(“cdsc”)
  o=c(“dfvv”)
  g= as.data.frame(rbind(e,f,l,o))

因此,对于“cde”,在 c 数据帧中拆分为 c、d、e、NA。对于“cde”,实际名称是 inter。现在我想搜索 c 然后 d 然后 e 从数据帧 g 中的“cde”中拆分出来。如果在 g 中的一行中找到 c,则在同一行中搜索 d 和 e,并分配与所有相邻的分数 100。当 NA 出现时,打破循环并转到下一行搜索,即 d、f、g。

输出应该是

  V0        V      V1  Score1   V2  Score2  V3  Score3   V4   Score4     V5
 inter     cde      c   100%     d   100%   e    100%    NA    0%       cdes
 travel    dfgh     d   100%     f   100%   g    100%    h     0%       dfgk

因此,在输出中,所有评分都已完成,它还给出了数据帧 g 中已执行匹配的匹配行。数据帧 g 中更匹配的一个应该在 V5 之下

4

1 回答 1

1

你在寻找这样的东西吗?

aux =apply(c,2,function(x){              # Run function for each column of c
  aux=rep("0%",nrow(g))                  # Create adjacent column with all 0%
  for (i in 1:nrow(g)){                  # For each row of g
    if (grepl(x[i],g[i,],fixed = TRUE)){ # If the letter is found in the text
      aux[i] = "100%"                    # update the 0% with 100%
    }
  }
  cbind(x,aux)                           # join 'c' column to the % column
})

dim(aux)=c(nrow(g),ncol(c)*2)            # reshape the results dimension

> aux
     [,1] [,2]   [,3] [,4]   [,5] [,6]   [,7] [,8]
[1,] "c"  "100%" "d"  "100%" "e"  "100%" "NA" "0%"
[2,] "d"  "100%" "f"  "100%" "g"  "100%" "h"  "0%"

请注意,我将代码概括为任何nrowncol

您可以添加名称

colnames(aux)=c(rbind(paste0("V",1:(ncol(aux)/2)),
                      paste0("Score",1:(ncol(aux)/2))))

编辑

我真的不明白你的部分代码的意义......你为什么需要ol??? 不过,这应该会给你你想要的结果。

g = as.data.frame(rbind(e,f),stringsAsFactors = FALSE)
c = as.data.frame(rbind(a,b)) 

aux =apply(c[,-(1:2)],2,function(x){                      # Run function for each column of c
  print(x)        
  aux=rep("0%",nrow(g))                          # Create adjacent column with all 0%
  for (i in 1:nrow(g)){                          # For each row of g
    is_it_in = grepl(x[i],g[i,],fixed = TRUE)
    if (ifelse(is.na(is_it_in),FALSE,is_it_in)){ # If the letter is found in the text
      aux[i] = "100%"                            # update the 0% with 100%
    }
  }
  cbind(x,aux)                                   # join 'c' column to the % column
})

dim(aux)=c(nrow(g),(ncol(c)-2)*2)                # reshape the results dimension
res = cbind(c[,1:2],aux,g)                       # Join everything
names(res) = c("V0","V",c(rbind(paste0("V",1:(ncol(res)/2)),
                           paste0("Score",1:(ncol(res)/2)))))[-(ncol(res)+1)]

> res
      V0    V V1 Score1 V2 Score2 V3 Score3   V4 Score4   V5
a  inter  cde  c   100%  d   100%  e   100% <NA>     0% cdes
b travel dfgh  d   100%  f   100%  g   100%    h     0% dfgk

当包含 l 和 o 时输出。然而,理想情况下,应该只有两行,因为它们是更接近的匹配。

  V0    V V1 Score1 V2 Score2 V3 Score3   V4 Score4    V5 Score5   V6
 inter  cde  c   100%  d   100%  e   100% <NA>     0% inter     0% cdes
 travel dfgh  d   100%  f   100%  g   100%    h     0%  <NA>     0% dfgk
 inter  cde  c     0%  d     0%  e     0% <NA>     0% inter     0% cdsc
 travel dfgh  d     0%  f     0%  g     0%    h     0%  <NA>     0% dfvv
于 2019-05-17T11:11:22.803 回答