【问题标题】:Multiple Pattern matching in R over multiple files , multiple columns & rowsR中的多个模式匹配多个文件,多列和多行
【发布时间】:2016-10-22 03:31:45
【问题描述】:

我有一个我需要从中读取的 CSV 文件列表,其中包含多个文件,其中包含标题、描述...等列。从多个文件的这些列中,必须编写检索操作并与另一个 CSV 相匹配,该 CSV 由类似于 WordStream SEO 的工具生成的流行关键字(~10k)生成。

我能做什么

#Not sure if this is correct approach
              Source1<- read.csv(path to csv file)
                Keywords_tomatch<- read.csv(path to csv file)

            #cant really take both the columns into single vector and iterate over them

                    subColdesc <- Source1[,c(3)]
                    subcolTitle <-Source1[,c(2)]
                   keywordget<- subset(Keywords_tomatch,grepl("*",Keywords_tomatch$col1))

    #Two individual vectors since i'm not sure whether sapply() can be applied over multiple lists     Definition: sapply(list,function)

            descBoolean <- sapply(keywordget, 
                                      function(y) 
                                        sapply(subColdesc , 
                                               function(x) 
                                                 any(grepl(y,x))) 
                               )

           TitleBoolean = sapply(keywordget, 
                                  function(y)
                                    sapply(subcolTitle , 
                                        function(x)
                                          any(grepl(y,x)))
                              )

#matches just the first element in the column of keywordget against (~4k) elements in description,title column. i.e returns a warning/error 

在 grepl(y, x) 中: 参数“模式”的长度 > 1,并且只使用第一个元素

我已经在 Akrun's version of grep 尝试过,但它对我不起作用

问题:

如何匹配keywordget向量中的所有元素,检索Description,Title每一行匹配了哪些列,Description和Title匹配了哪些行。

简而言之,如何使用Keywords_tomatch检索Source1中所有游戏相关产品?

作为示例,我发布了我收集的两个文件。 Source1 只包含几行 4k 行

来源1 =1.csv, Keywords_tomatch = Gaming.csv

【问题讨论】:

    标签: r pattern-matching


    【解决方案1】:

    首先,让我指出一些可能导致您的代码无法正常工作的原因(如果我错了,请纠正我):

    • 您的文件是用stringAsFactors = TRUE 读取的,grepl 确实识别pattern = 参数的因子变量。但是由于您没有收到关于 grepl 无法识别因子的错误,因此我假设您在匹配之前将它们转换为字符。

    • grepl 需要 fixed = TRUE 参数,否则它将把键的元素视为正则表达式。

    • 您的关键字get 是一个数据框,R 将数据框作为列表调用时将其视为列表。因此,由于sapply 的第一个参数采用了一个列表,因此它将keywordget 视为具有1 个元素的列表。所以当这个元素(本质上是keywordget的整个向量)被提供给grepl函数的pattern参数时,你会得到错误: 在 grepl(y, x) 中:参数 'pattern' 的长度 > 1,并且只会使用第一个元素

    例如,这应该可以工作:

    sapply(keywordget$GAMING, function(y) {
      sapply(source1$title, function(x) {
        any(grepl(y,x, fixed = TRUE))
      })
    })
    

    以下是我的解决方案:

    # Read files
    source1 = read.csv("source1.csv", stringsAsFactors = FALSE)
    keys = read.csv("gaming.csv", stringsAsFactors = FALSE)
    
    # Finds the index of elements in source1 that matches 
    # with any of the keys
    matchIndex = lapply(source1, function(x){
      which(Reduce(`|`, lapply(keys$GAMING, grepl, x, fixed = TRUE)))
    })
    
    > matchIndex
    $title
    integer(0)
    
    $description
    [1] 189 293 382 402 456
    

    title 有 0 个匹配项,而 description 有 5 个匹配项

    # Returns the descriptions that match
    source1$description[matchIndex$description]
    
    # Returns the title corresponding to the descriptions that match
    source1$title[matchIndex$description]
    
    > source1$title[matchIndex$description]
    [1] "tomb raider: legend"                     
    [2] "namco museum 50th anniversary collection"
    [3] "restricted area"                         
    [4] "south park chef's luv shack"             
    [5] "brainfood games cranium collection 2006" 
    

    【讨论】:

    • 感谢您的回复。我已经尝试使用 Reduce 和 lapply 以及 R-Bloggers 方法来实现上述理论来解决问题,尽管我只能匹配总数的 45%字符串。
    猜你喜欢
    • 2022-01-07
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 2016-04-24
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多