mardi 11 juillet 2017

R function: for loop within if statement, parenthesis error

I'm trying to write a function to iterate through column X, find which rows contain X[i], and change the value of Y for those rows to 'referral':

fuzlook <- function(x) {
 for (i in 1:100){
  if (length(agrep(b$Name[i], b$Referal)>0)){
   GG=agrep(b$Name[i], b$Referal)
   for (q in 1:length(GG) {
    b$Referal[GG[q]]<- 'referral'
   }
  } 
 }
}

I know that I'm a beginner and my code is probably inefficient, but I am more concerned with the error message I get when I run this:

Error: unexpected '}' in " }"

I quadruple checked that the parenthesis line up. However, the code ran before I added this part: for (q in 1:length(GG) { b$Referal[GG[q]]<- 'referral' }
so my guess is that is has to do with nesting a for-loop within an if-statement (but then why would it return a parenthesis error??)

Aucun commentaire:

Enregistrer un commentaire