[R] How do you use agrep inside a loop

Rui Barradas ruipbarradas at sapo.pt
Tue Dec 11 12:43:03 CET 2012


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:
> Pascal,
>
> result_vector <- vector()
> result <- vector("list", (length(test1)-1))
> for(i in 1:(length(test1)-1))
> {
>    for(j in (i+1):length(test1))
>    {
>    result_vector[j-i] <- agrep(test1[i], test1[j], ignore.case = TRUE, value
> = TRUE, max.distance = 0.1)
>    }
>    result[[i]]<- result_vector
> }
>
> I'm not sure what the problem is with the dimension/length of result which
> is a list. But I just use the second line: result <- vector("list",
> (length(test1)-1))
>
> What am I missing?
>
> Thank you Rui Barradas.
>
> On Tue, Dec 11, 2012 at 4:25 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
>> 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:
>>
>>   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 :
>>>
>>>> Hi all.
>>>>
>>>> This is my first message at R-help...so I'm hoping I have some beginner's
>>>> luck and get some good help for my problem!
>>>>
>>>> FYI I have just started using R recently so my knowledge of R is pretty
>>>> preliminary.
>>>>
>>>> Okay here is what I need help with - I need to know how to use agrep in a
>>>>    for loop.
>>>>
>>>> I need to compare elements of a vector of names with other elements of
>>>> the
>>>> same vector.
>>>>
>>>> However if I use something like this:
>>>>
>>>> for(i in 1:length(test1)-1)
>>>> {
>>>>     for(j in i+1:length(test1))
>>>>     {
>>>>     result[[i]][j] <- agrep(test1[i], test1[j], ignore.case = TRUE, value
>>>> =
>>>> TRUE, max.distance = 0.1)
>>>>     }
>>>>
>>>> }
>>>>
>>>> I get an error message saying - invalid 'pattern' argument. -* Error in
>>>> agrep(test1[i], test1[j], ignore.case = TRUE, value = TRUE, max.distance
>>>> =
>>>> 0.1) : *
>>>> *  invalid 'pattern' argument*
>>>>
>>>> Test 1 being - c("Vashi", "Vashi,navi Mumbai", "Thane", "Vashi,new
>>>> Mumbai",
>>>> "Thana", "Surekha", "Thane(w)", "surekhaN")
>>>>
>>>> This is the first time I'm using agrep, I do not understand how it works
>>>> fully...
>>>>
>>>> Kindly help...
>>>>
>>>> Thank you.
>>>>
>>>> Su.
>>>>
>>>>      [[alternative HTML version deleted]]
>>>>
>>>> ______________________________**________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>>> PLEASE do read the posting guide http://www.R-project.org/**
>>>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>




More information about the R-help mailing list