2
$\begingroup$

I am trying to reproduce the example of Data Cleaning from here. I had to make a slight change in input 13 to

j[x_] := If[StringMatchQ[x, "TOTAL*"], x, DateList[x]];

After running input 14 I get the following data:

data

There is a TOTAL row for every year. The article proposes to delete those rows by running (asterisk is my contribution)

tmp4 = DeleteCases[tmp3, {"TOTAL*", __}];

but it doesn't work! After searching StackExchange, I found that DeleteCases is not compatible with string patterns.

So how do we delete those rows? And if DeleteCases is truly incompatible, how did the Mathematica article succeed?

PS: I did manage to delete those rows by sorting the data and dropping them, but I'd still like to know how to do it with DeleteCases.

Thanks,

Thad

$\endgroup$
1
$\begingroup$

Using a simpler example

tmp3 = {{"TOTAL*", 1, 2}, {"TOTAL123", 1, 2}, {a, b, c}};

Your code deletes only the first element in which the first entry literally matches "TOTAL*"

DeleteCases[tmp3, {"TOTAL*", __}]
{{"TOTAL123", 1, 2}, {a, b, c}}

To get the desired result you need to specify the first element of list in the pattern to be matched as a String which StringMatches the string pattern "TOTAL*". You can do this using PatternTest (?) or using Condition (/;):

DeleteCases[tmp3, {_String?(StringMatchQ[#, "TOTAL*"] &), __}] (* or *)
DeleteCases[tmp3, {s_String /; StringMatchQ[s, "TOTAL*"], __}]

{{a, b, c}}

$\endgroup$

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.