Skip to content

How do you use agrep inside a loop

11 messages · surekha nagabhushan, Pascal Oettli, Rui Barradas

#
Hi,

There is a mistake in the first line. It should be:
 > for(i in 1:(length(test1)-1))

Regards,
Pascal


Le 11/12/2012 16:01, surekha nagabhushan a ?crit :
#
Hello,

Probably because 'result' doesn't have the expected size/number of 
dimension. How do you create it?

Regards,
Pascal


Le 11/12/2012 19:46, surekha nagabhushan a ?crit :
#
Hello,

And another error in line 2. It should be

for(j in (i+1):length(test1))


Hope this helps,

Rui Barradas

Em 11-12-2012 07:54, Pascal Oettli escreveu:
#
Hello,

Right, I didn't notice. Apologies.

Pascal


Le 11/12/2012 19:55, Rui Barradas a ?crit :
#
Hello,

See if this is it. You must reinitialize 'result_vector' just before the 
loop that constructs it.


test1 <- c("Vashi", "Vashi,navi Mumbai", "Thane", "Vashi,new Mumbai",
         "Thana", "Surekha", "Thane(w)", "surekhaN")

result <- vector("list", (length(test1)-1))
for(i in 1:(length(test1)-1)){
     result_vector <- vector()
     for(j in (i+1):length(test1)){
         tmp <- agrep(test1[i], test1[j],
                     ignore.case = TRUE, value = TRUE,
                     max.distance = 0.1)
         if(length(tmp) > 0) result_vector[j-i] <- tmp
     }
     result[[i]] <- result_vector
}
result


Hope this helps,

Rui Barradas
Em 11-12-2012 11:23, surekha nagabhushan escreveu:
#
Hello,

Inline.
Em 11-12-2012 12:04, surekha nagabhushan escreveu:
No! This must be just before the loop in 'j'
Yes, without testing for length zero it throws an error, "replacement 
has length zero".

Hope this helps,

Rui Barradas
1 day later