0

我想使用 agrep 函数提取相似的文本字符串并将它们保存在列表或向量中,但结果的长度不同(即使替换的长度可能为零),所以我得到一个错误。

如何定义列表或向量以保存结果,即使它们的长度不同?

这是一个可重现的示例:

x <- c("REF.E600J","SIN MODELO","REF.E705N","24-53793A-K","24-53646A-K","33-53633A-K",
   "REF.E522N","CON MODELO","VAR MODELO","REF.E610L")

similitud <- list()

for (i in c(1:length(x))) {
similitud[i] <- agrep(x[i],x[-i],max=3,value=T)
}
#Error and warning
Error in similitud[i] <- agrep(x[i], x[-i], max = 3, value = T) : 
replacement has length zero
In addition: Warning messages:
1: In similitud[i] <- agrep(x[i], x[-i], max = 3, value = T) :
number of items to replace is not a multiple of replacement length
2: In similitud[i] <- agrep(x[i], x[-i], max = 3, value = T) :
number of items to replace is not a multiple of replacement length
3: In similitud[i] <- agrep(x[i], x[-i], max = 3, value = T) :
number of items to replace is not a multiple of replacement length
4

1 回答 1

1

对于列表,您[[不使用[分配/获取单个元素([返回子列表)。

for (i in c(1:length(x))) {
  similitud[[i]] <- agrep(x[i],x[-i],max=3,value=T)
}

只需将您的更改similitud[i]similitud[[i]].

于 2015-07-15T04:38:41.033 回答