4
$\begingroup$
data={{one,a,16,2},{one,b,18,2},{one,c,28,3},{one_ave,d,45,4},{two,a,47,2},{two,b,8,6},{two,c,33,17},{two_ave,d,4,4}};

For the matrix above, I want to select all those rows in which the first column contains the text "ave" (i.e. rows 4 and 6). To get those columns containing "two_ave"...

Select[data, #[[1]] == two_ave &]

...obviously works. I was hoping replacing the "two' with a wildcard might get the job done, but I can't seem to get it to work. I've had a good search around but can't find the answer. Can anybody help?

$\endgroup$
6
$\begingroup$
Select[StringMatchQ["*ave"] @* ToString @* First][data]
 {{one_ave, d, 45, 4}, {two_ave, d, 4, 4}}
$\endgroup$
4
  • $\begingroup$ you enormous legend. thanks! $\endgroup$
    – Tim J
    May 1 '20 at 10:47
  • $\begingroup$ @TimJ, you are welcome. And welcome to mma.se. $\endgroup$
    – kglr
    May 1 '20 at 10:48
  • $\begingroup$ if might i bother you with a follow up, how do i generate the matrix that excludes those rows containing "ave". i was hoping (again) that replacing 'Select' with 'Drop' or 'DeleteCases' might do it, but i was wrong (again). $\endgroup$
    – Tim J
    May 2 '20 at 2:50
  • $\begingroup$ @TimJ, you can use Select[Not@*StringMatchQ["*ave"]@*ToString@*First][data] to exclude rows containing "*ave". $\endgroup$
    – kglr
    May 2 '20 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.