jeudi 4 avril 2019

Create a new column containing the grepped string

I have dataframe of 3500+ projects and I want to grep search for 40ish key words in the Project_Description column. If the Project_Description contains one or more of the key words, I want to create a new column and label that project's row with the key word(s).

How can I create an if statement that loops through my key words and if the key word(s) is found labels the proper row with the key word(s)? Especially if the Project_Description probably contains more than one of the key words?

So far I've been able to pull out the rows of projects that contain at least one of the key words in the Project_Description column.

key_words <- c("who","what","when","where","why", etc...)

dataframe_key_words <- c()

for (i in 1:length(key_words)){
dataframe_key_words <- rbind(dataframe_key_words, dataframe_original[grep(key_words[i], dataframe_original$Project_Description), ]
}

Aucun commentaire:

Enregistrer un commentaire