r - flexible patterns for a factor variable in order to subset a dataframe -


i have dataframe called mydf, simplified below:

mydf  var1                          var2 abc_color1_location1_number1  1000 xyz_color1_location1_number1  100 asd_color2_location2_number1  900 qwe_color1_location1_number2  200 sdf_color2_location1_number2  1100 qwerrrr_ahjkkk_asdfgggg       234   sdf_color1_location2_number1  3577 abc_color1_location3_number1  86544 

i want subset dataset flexibly based on var1 example:

pattern <- c("abc", "color1", "number1") newmydf <- mydf[grep(paste("_",paste(pattern,collapse="_|_"),"_",sep=""),mydf$var1,ignore.case=t),] 

my expected result:

newmydf var1                          var2 abc_color1_location1_number1  1000 

however, resulted dataframe being subset pattern "abc" , "color1" only, while want patterns should considered. can please me in case?

many in advance!

with kind regards,

if want elements of pattern considered, might help:

pattern <- c("abc", "color1", "number1") alltrue <- rowsums(sapply(pattern, function(x) grepl(pattern = x, mydf$var1))) == 3  mydf[alltrue, ] #                          var1  var2 #1 abc_color1_location1_number1  1000 #8 abc_color1_location3_number1 86544 

essentially sapply run grepl each 1 of pattern elements , use ones grepls true.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -