【问题标题】:Replace duplicates in matrix替换矩阵中的重复项
【发布时间】:2021-06-03 12:43:20
【问题描述】:

我为您准备了以下测试代码:

####TESTING HERE
test = tibble::tribble(
                          ~Name1,           ~Name2,          ~Name3,
                   "Paul Walker",    "Paule Walkr",   "Heiko Knaup",
                "Ferdinand Bass", "Ferdinand Base", "Michael Herre"
                )

library(stringdist)
output <- list()
for (row in 1:nrow(test)) 
{
  codephon = phonetic(test[row,], method = c("soundex"), useBytes = FALSE)
  output[[row]] <- codephon
}

#building the matrix with soundex input
phoneticmatrix = matrix(output)
soundexspalten=str_split_fixed(phoneticmatrix, ",", 3)
#> Error in str_split_fixed(phoneticmatrix, ",", 3): konnte Funktion "str_split_fixed" nicht finden
soundexmatrix0 = gsub('[()c"]', '', soundexspalten)
#> Error in gsub("[()c\"]", "", soundexspalten): Objekt 'soundexspalten' nicht gefunden
soundexmatrix1 = gsub("0000", "", soundexmatrix0)
#> Error in gsub("0000", "", soundexmatrix0): Objekt 'soundexmatrix0' nicht gefunden

reprex package (v2.0.0) 于 2021 年 6 月 3 日创建

现在我想!!!用字符串“DUPLICATE”替换 soundexmatrix1 中的所有重复项,以便矩阵的维度保持不变,并且可以立即看到所有重复项。

任何想法如何做到这一点? 感谢您的帮助!

【问题讨论】:

    标签: r matrix duplicates stringdist


    【解决方案1】:

    要检查每一行中的重复项(请参阅更新),这应该可以实现您想要的,并且以更简洁的方式:

    # Feel free to load the packages you're using.
    # library(stringdist)
    # library(tibble)
    
    test <- tibble::tribble(
      ~Name1,           ~Name2,           ~Name3,
      "Paul Walker",    "Paule Walkr",    "Heiko Knaup",
      "Ferdinand Bass", "Ferdinand Base", "Michael Herre"
    )
    
    # Get phonetic codes cleanly.
    result <- as.matrix(apply(X = test, MARGIN = 2,
                              FUN = stringdist::phonetic, method = c("soundex"), useBytes = FALSE))
    
    # Find all blank codes ("0000").
    blanks <- result == "0000"
    
    # # Find all duplicates, as compared across ENTIRE matrix; ignore blank codes.
    # all_duplicates <- !blanks & duplicated(result, MARGIN = 0)
    
    # Find duplicates, as compared within EACH ROW; ignore blank codes.
    row_duplicates <- !blanks & t(apply(X = result, MARGIN = 1, FUN = duplicated))
    
    # Replace blank codes ("0000") with blanks (""); and replace duplicates (found
    # within rows) with "DUPLICATE".
    result[blanks] <- ""
    result[row_duplicates] <- "DUPLICATE"
    
    # View result.
    result
    

    result 应该是以下矩阵:

         Name1  Name2       Name3 
    [1,] "P442" "DUPLICATE" "H225"
    [2,] "F635" "DUPLICATE" "M246"
    

    更新

    根据张贴者的request,我已更改代码以仅在每一行内比较重复项,而不是在整个result 矩阵中进行比较。现在,test 数据集就像

    test <- tibble::tribble(
        ~Name1,           ~Name2,           ~Name3,
        "Paul Walker",    "Paule Walkr",    "Heiko Knaup",
        "Ferdinand Bass", "Ferdinand Base", "Michael Herre",
        "",               "01234 56789",    "Heiko Knaup"
    # | ^^              | ^^^^^^^^^^^^^   | ^^^^^^^^^^^^^                   |
    # | Coded as "0000" | Coded as "0000" | Duplicate in matrix, NOT in row |
    )
    

    会给result点赞

         Name1  Name2       Name3 
    [1,] "P442" "DUPLICATE" "H225"
    [2,] "F635" "DUPLICATE" "M246"
    [3,] ""     ""          "H225"
    

    【讨论】:

    • 您好,这很有帮助,但我只想在同一行中有重复项而不是整个矩阵中的所有重复项时放置“DUPLICATE”。
    • 啊,我想我有这个东西。给我几个小时。我假设你是在中欧夏令时,@MaxH。?
    • 嗨@MaxH。我刚刚更新了代码以仅在行内比较重复项。
    • 请不要添加更新,只需编辑以成为当时最好的帖子。如果一个问题被编辑以使合理的答案无效,请将其回滚。 help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多