【问题标题】:" 'pattern' must be a non-empty character string" error with agrep in R“'模式'必须是非空字符串”R中的agrep错误
【发布时间】:2014-02-20 00:00:02
【问题描述】:

我收到以下错误:

'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 之间的整数。

【问题讨论】:

  • 代码是可重现的,但是数据集丢失了,所以错误无法重现。

标签: r lapply sapply agrep


【解决方案1】:

我可以通过向模式提供"" 来获取该错误消息。如此处所示,但没有其他潜在的不良模式:

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

所以我猜你在 Database` 中有一个 ""。检查使用:

which(Database1 == "")

编辑

用途:

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

这将告诉您错误在哪里,然后您可以仔细研究该元素并找出导致错误的原因。我会尝试数据的多个子集。

【讨论】:

  • 太棒了!它包含很多“”,所以我将它设置为一个变量:remove
  • 我会一直玩下去。再次感谢!
  • 成功了!再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2022-01-16
  • 2022-07-15
相关资源
最近更新 更多