'R: Removing Text using gsub in loop [closed]

I'm trying to parse and clean up a string in a column named Tab in a dataframe named df3 in R. Here is my solution adopting the gsub() function in R.

df3$Tab <- gsub(".*from","",df3$Tab)
df3$Tab <- gsub(".*FROM","",df3$Tab)
df3$Tab <- gsub("where.*","",df3$Tab)
df3$Tab <- gsub("WHERE.*","",df3$Tab)

basically I'd like to remove all before and up to "FROM" as well as everything behind "WHERE". This solution works but I'd like to write down a loop, but when I do this:

df3$Tab <- for (i in seq_along(df3$Tab)){
  df3$Tab <- gsub(".*from","",df3$Tab)
  df3$Tab <- gsub(".*FROM","",df3$Tab)
  df3$Tab <- gsub("where.*","",df3$Tab)
  df3$Tab <- gsub("WHERE.*","",df3$Tab)
  break
}

I completely delete the df3$Tab column. Is the anybody that can explain me where I'm wrong? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source