I am attempting to subset a data frame by removing rows containing certain charater patterns, which are stored in a vector. My issue is that only the last pattern of the vector is removed from my data frame. How can I make my loop work iteratively, so that all patterns stored in the vector are removed from my data frame?
Mock input:
df<-data.frame(organism=c("human_longname","cat_longname","bird_longname","virus_longname","bat_longname","pangolian_longname"),size=c(6,4,2,1,3,5))
df
organism size
1 human_longname 6
2 cat_longname 4
3 bird_longname 2
4 virus_longname 1
5 bat_longname 3
6 pangolian_longname 5
used code and output:
vectors<-c("bat","virus","pangolian")
for(i in vectors){df_1<-df[!grepl(i,df$organism),]}
df_1
organism size
1 human_longname 6
2 cat_longname 4
3 bird_longname 2
4 virus_longname 1
5 bat_longname 3
Expected output
df_1
organism size
1 human_longname 6
2 cat_longname 4
3 bird_longname 2