[R] How do you use agrep inside a loop

Pascal Oettli kridox at ymail.com
Tue Dec 11 11:51:59 CET 2012


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 :
> Thank you for that Pascal.
>
> I changed this bit to the following:
>
> result <- vector("list", (length(test1)-1))
> for(i in 1:(length(test1)-1))
> {
>    for(j in i+1:length(test1))
>    {
>    result[[i]][j-i] <- agrep(test1[i], test1[j], ignore.case = TRUE,
> value = TRUE, max.distance = 0.1)
>    }
> }
>
> And now I get the error - Error in result[[i]][j - i] <- agrep(test1[i],
> test1[j], ignore.case = TRUE,  :
>    replacement has length zero
>
> What does this mean?
>
> On Tue, Dec 11, 2012 at 1:24 PM, Pascal Oettli <kridox at ymail.com
> <mailto:kridox at ymail.com>> wrote:
>
>     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 <mailto: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