0

我想将我的第一个数据集中的字符串与所有最接近的常见匹配项进行匹配。

数据如下:

数据集1:

California 
Texas 
Florida 
New York

数据集2:

Californiia 
callifoornia
T3xas
Te xas
texas
Fl0 rida
folrida
New york
new york

期望的结果是:

col_1                col_2              col_3            col4
California           Californiia        callifoornia
Texas                T3xas              texas            Te xas
Florida              folrida            Fl0 rida
New York             New york           new york

问题是:

  • 如何搜索第一个数据集和第二个数据集之间的公共字符串,并在第二个数据集中生成与第一个数据集中的每个术语对齐的术语列表?

提前致谢。

4

2 回答 2

0

我读了一些关于 stringdist 的文章并想出了这个。这是一种解决方法,但我喜欢它。绝对可以改进:

library(stringdist)
library(janitor)

ds1a <- read.csv('dataset1')
ds2a <- read.csv('dataset2')

distancematrix <- stringdistmatrix(ds2a$name, ds1a$name, useNames = T)


df <- data.frame(stringdistmatrix(ds2a$name, ds1a$name, useNames = T), ncol=maxcol in distance matrix)
# go thru this df, and every cell that's < 4, replace with the column name, otherwise replace with empty string

for (j in 1:ncol(df)) {
      trigger <- df[j,] < 4
      df[trigger , j] <- names(df)[j]
      df[!trigger , j] <- ""
}


df <- remove_constant(df)

write.csv(df, file="~/Desktop/df.csv")
于 2019-04-24T17:48:39.123 回答
0
library(fuzzyjoin); library(tidyverse)
dataset1 %>%
  stringdist_left_join(dataset2, 
                       max_dist = 3) %>%
  rename(col_1 = "states.x") %>%
  group_by(col_1) %>%
  mutate(col = paste0("col_", row_number() + 1)) %>%
  spread(col, states.y)

#Joining by: "states"
## A tibble: 4 x 4
## Groups:   col_1 [4]
#  col_1      col_2       col_3        col_4
#  <chr>      <chr>       <chr>        <chr>
#1 California Californiia callifoornia NA   
#2 Florida    Fl0 rida    folrida      NA   
#3 New York   New york    new york     NA   
#4 Texas      T3xas       Te xas       texas

数据:

dataset1 <- data.frame(states = c("California",
                                "Texas",
                                "Florida",
                                "New York"), 
                       stringsAsFactors = F)

dataset2 <- data.frame(stringsAsFactors = F,
  states = c(
    "Californiia",
    "callifoornia",
    "T3xas",
    "Te xas",
    "texas",
    "Fl0 rida",
    "folrida",
    "New york",
    "new york"
  )
)
于 2019-04-23T06:12:58.480 回答