Question

I am trying to repeat two loops multiple times until a condition is met

The first loop is to remove first instances of a keyword(pattern) from a string. The second loop is to count the number of instances of keywords in the same string.

The data is in a dataframe with 3 columns - string, keyword and number of times keyword is repeated in string (NoKW)

string <- c (" temple san temple lush ", " mohito sudoku war ", " martyr  martyr metal martyr", " jump statement statement ", " window capsule turn ")
keyword <- c (" temple ", " sudoku ", " martyr " , " statement ", " capsule ")
NoKW <- c(2,1,3,2,1)
data <- data.frame (string, keyword, NoKW)
data$string <- as.character(data$string)
data$keyword <- as.character(data$keyword)

The idea is to sequentially remove instances of keywords until I have only one instance of the keyword in the corresponding string.

I have tried to use repeat as follows.

repeat
{
M <- nrow(data);
for (j in 1:M){
if(1 < data[j,3]) data[j,1] <- str_replace(data[j,1], data[j,2], " ")
};
for (i in 1:M){
data[i,3] <- sum(str_count(data[i,1], data[i,2]))
};
max <- as.numeric(max(data$NoKW));
if (max = 1)
break;
}

But it gives the following errors

Error: unexpected '=' in:
"    };
if (max ="
>         break;
Error: no loop for break/next, jumping to top level
> }
Error: unexpected '}' in "}"
>

I am new to R loops so can you tell me where I am going wrong.

Was it helpful?

Solution

The idea is to sequentially remove instances of keywords until I have only one instance of the keyword in the corresponding string.

You don't need a for loop:

#split your strings by space
substrings <- strsplit(string, " ", fixed=TRUE)

#remove spaces from keywords
keyword_clean <- gsub("\\s", "", keyword)

#loop over the strings
sapply(substrings, function(s) {
  #which word is duplicated and a keyword
  rm <- which(duplicated(s, fromLast=TRUE) & s %in% keyword_clean)  
  #remove the duplicated keywords
  if (length(rm > 0)) s <- s[-rm]
  #paste words together
  paste(s, collapse=" ")
  })

#[1] " san temple lush"     " mohito sudoku war"   "  metal martyr"       " jump statement"      " window capsule turn"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top