在R

时间:2015-07-15 04:22:36

标签: r agrep

我想使用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

1 个答案:

答案 0 :(得分:1)

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

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

只需将similitud[i]更改为similitud[[i]]