I'm looking for a way to delete an element out of a data frame (delete the full row) if a certain word is not found in the Name column. In my case, the element is a color like red,blue,green
The dataset looks like this:
Name|Classification
A red apple | Fruit
A banana | Fruit
A blue carrot | Vegetable
I use the following code to loop through the dataframe and check if a color is in the string of Name column. Currently only looking at red to make it work.
for(i in 1:length(nsl)){
if(!grepl("red",nsl[[1:i]],fixed=TRUE)){
nsl[[1:i]] <- " " #This is where I want to delete the item or set it to empty (so later on I can filter all empty out)
}
}
But I have no clue how it is done on a data frame that has two columns/dimensions. All internet tutorials I find tell me to use nsl[-index_element_here]
but that deletes the column Classification
. Which they use on examples of 1-D lists. Or tell me to use %in%
which is , I think, not what I'm looking for.
The expected end result is a dataframe where only fruit and vegetables with color are in. Non-colour is deleted.