Search code examples
rdataframeloopstwitter

Finding tweets containg a keyword in R and updating


Im using R in order to find words in strings that way i can categorize them in 2 columns. Im using grep to find all of the rows where the keyword exists, but im having trouble writing a loop to update another column with a value of 1 if the keyword exists in the string. Tweet.Text is the column name containing the strings, @casekeenum7 is the keyword im looking for and Keenum is the column that I would like to update with a value of 1 for each row that contains the keyword.

Code:

for (i in 1:length(Tweet.Text)){
  if(grep('@casekeenum',Tweet.Text[i])){
    Keenum[i]==1
  }
}

Error:

Error in if (grep("@casekeenum7", Tweet.Text[i])) { : 
  argument is of length zero

Solution

  • You do not need a for loop:

    df = data.frame(Tweet.Text=c("Hello","@casekeenum"),
                    Keenum=c(0,0))
    
    
    df$Keenum[grepl("@casekeenum",df$Tweet.Text)]=1
    

    returns:

       Tweet.Text Keenum
    1       Hello      0
    2 @casekeenum      1