代码之家  ›  专栏  ›  技术社区  ›  tomathon

“'pattern'必须是非空字符串”错误,R中有agrip

  •  0
  • tomathon  · 技术社区  · 12 年前

    我收到以下错误:

    'pattern' must be a non-empty character string 
    

    尝试运行以下操作时:

    rapply(as.list(Database1), function(x) agrep(x,Database2, max.distance=c(cost=1), value=T))
    

    具有大型数据库

    > length(Database1)
    [1] 15876500
    
    > length(Database2)
    [1] 605
    

    但当我用小号跑步时就不行了

    > length(Database1)
    [1] 29
    
    > length(Database2)
    [1] 8
    

    我知道我应该建立可复制的代码,这样数据库就只有15-25个随机字母的字符串,可以使用以下方法生成:

    Database1<- unlist(replicate(n, paste0(sample(LETTERS, m), collapse="")))
    

    其中“n”是长度,“m”是15-25之间的整数。

    1 回复  |  直到 12 年前
        1
  •  3
  •   Tyler Rinker DaniM    12 年前

    我可以通过提供 "" 到模式。如这里所见,但没有其他潜在的坏模式:

    agrep("", "hello")
    agrep(" ", "hello")
    agrep(NA, "hello")
    agrep(NULL, "hello")
    
    
    ## > agrep("", "hello")
    ## Error in agrep("", "hello") : 
    ##   'pattern' must be a non-empty character string
    
    ## > agrep(" ", "hello")
    ## [1] 1
    
    ## > agrep(NA, "hello")
    ## [1] NA
    
    ## > agrep(NULL, "hello")
    ## Error in agrep(NULL, "hello") : invalid 'pattern' argument
    

    所以我猜你有 "" 在数据库中`。检查使用情况:

    which(Database1 == "")
    

    编辑 :

    使用:

    rapply(as.list(Database1), function(x) {
        try(agrep(x,Database2, max.distance=c(cost=1), value=T))
    )
    

    这将告诉您错误的位置,然后您可以仔细研究该元素并找出导致错误的原因。