6
$\begingroup$

I have data as a list of four element lists, some with missing data, thus

{{214.1699982, 113.5199966, "miss", 117.}, {214.0899963, 124.4199982, 
  "miss", 129.}, {195.4199982, 110.3600006, 79., 123.}, {"miss", 
  125.6399994, 77., 127.}, {197.9900055, "miss", 84., 
  119.}, {213.3800049, 122.4100037, 80., 123.}, {"miss", 126.2699966, 
  83., 123.}, {"miss", 113.5400009, 78., 128.}, {"miss", 130.9700012, 
  81., 126.}, {208.8800049, "miss", 79., 113.}}

I need to count which have full data and which are missing for each element position, like this enter image description here

Have tried Select, Pick, Cases with no success

$\endgroup$

6 Answers 6

2
$\begingroup$

For the data:

d = {{214.1699982, 113.5199966, "miss", 117.}, {214.0899963, 
    124.4199982, "miss", 129.}, {195.4199982, 110.3600006, 79., 
    123.}, {"miss", 125.6399994, 77., 127.}, {197.9900055, "miss", 
    84., 119.}, {213.3800049, 122.4100037, 80., 123.}, {"miss", 
    126.2699966, 83., 123.}, {"miss", 113.5400009, 78., 
    128.}, {"miss", 130.9700012, 81., 126.}, {208.8800049, "miss", 
    79., 113.}};

we define the following patterns, allowing for at most 2 misses:

pattern = {
  {_?NumericQ, _?NumericQ, _?NumericQ, _?NumericQ}, 
  {_?NumericQ, _ NumericQ, _?NumericQ, "miss"},
  {_?NumericQ,"miss", _?NumericQ, _?NumericQ}, 
  {"miss", _?NumericQ, _?NumericQ, _?NumericQ},
  {_?NumericQ, _?NumericQ, "miss", "miss"},
  {_?NumericQ, "miss", _?NumericQ, "miss"},
  {"miss", _?NumericQ, _?NumericQ, "miss"},
  {_?NumericQ, "miss", "miss", _?NumericQ},
  {"miss", _?NumericQ, "miss", _?NumericQ},
  {"miss", "miss", _?NumericQ, _?NumericQ}
  }

Then we count how many times each pattern appears:

co = Count[d, #] & /@ pattern;

Then for the displayt we change the patterns to 0 and 1's:

patdis = StringReplace[
    ToString[pattern], {"_?NumericQ" -> "1", "miss" -> "0"}] // 
   ToExpression;

And here is the result:

Grid[Prepend[Transpose[{co, patdis}], {"Frequency", "Pattern"}]]

enter image description here

$\endgroup$
7
$\begingroup$
tuples = Tuples[{0, 1}, Length @ First @ d];

boole = Boole @ Map[NumericQ, d, {-1}];

Prepend[{"pattern", "count"}] @ Transpose[{tuples, Count[boole, #] & /@ tuples}] // 
 Grid[#, Dividers -> {Center, {False, True, {False}}}] &

enter image description here

$\endgroup$
2
$\begingroup$
 data={{214.1699982, 113.5199966, "miss", 117.}, {214.0899963, 124.4199982, 
  "miss", 129.}, {195.4199982, 110.3600006, 79., 123.}, {"miss", 
  125.6399994, 77., 127.}, {197.9900055, "miss", 84., 
  119.}, {213.3800049, 122.4100037, 80., 123.}, {"miss", 126.2699966, 
  83., 123.}, {"miss", 113.5400009, 78., 128.}, {"miss", 130.9700012, 
  81., 126.}, {208.8800049, "miss", 79., 113.}}



 Tally[Replace[data, {_Real -> 1, _String -> 0}, Infinity]] // Grid

Output

{1,1,0,1} 2

{1,1,1,1} 2

{0,1,1,1} 4

{1,0,1,1} 2

$\endgroup$
1
$\begingroup$

A solution with Association and Dataset:

dt = 
    {{214., 113., "miss", 117.}, {214., 124., "miss", 129.},
     {195., 110.,79., 123.}, {"miss", 125., 77., 127.}, 
     {197., "miss", 84., 119.}, {213., 122., 80., 123.}, 
     {"miss", 126., 83., 123.}, {"miss", 113., 78., 128.}, 
     {"miss", 130., 81., 126.}, {208., "miss", 79., 113.}};

ds = <|Thread[Tuples[{0, 1}, 4] -> 0], Counts[dt /. {_Real -> 1, _String -> 0}]|>;

<|"Pattern" -> "Count", #, "Total" -> Total @ #|> & @ ds //
  Dataset[#, Alignment -> Right, HeaderBackground -> White] &

enter image description here

$\endgroup$
1
$\begingroup$

Using Lookup:

d = {{214.1699982, 113.5199966, "miss", 117.}, {214.0899963, 
    124.4199982, "miss", 129.}, {195.4199982, 110.3600006, 79., 
    123.}, {"miss", 125.6399994, 77., 127.}, {197.9900055, "miss", 
    84., 119.}, {213.3800049, 122.4100037, 80., 123.}, {"miss", 
    126.2699966, 83., 123.}, {"miss", 113.5400009, 78., 
    128.}, {"miss", 130.9700012, 81., 126.}, {208.8800049, "miss", 
    79., 113.}};

(lut = d /. "miss" -> 0 // Unitize // GroupBy[#, Identity, Length] &);
t = Tuples[{0, 1}, 4];

res = {#, First@Lookup[lut, {#}, 0]} & /@ t

    
Prepend[res, {"pattern", "count"}] // 
  Append[{"Total", res // Map[Last] // Total}] // 
 Grid[#, Dividers -> All] &

enter image description here

$\endgroup$
1
$\begingroup$

Using data:

d = {{214.1699982, 113.5199966, "miss", 117.}, {214.0899963, 
    124.4199982, "miss", 129.}, {195.4199982, 110.3600006, 79., 
    123.}, {"miss", 125.6399994, 77., 127.}, {197.9900055, "miss", 
    84., 119.}, {213.3800049, 122.4100037, 80., 123.}, {"miss", 
    126.2699966, 83., 123.}, {"miss", 113.5400009, 78., 
    128.}, {"miss", 130.9700012, 81., 126.}, {208.8800049, "miss", 
    79., 113.}};

Using Unitize, GroupBy, Lookup

Module[{a, f}, 
 a = {Row[#, "  "], 
     Lookup[KeyMap[f, 
       GroupBy[Unitize[d /. "miss" -> 0], # &, Length]], f[#], 0]} & /@
    IntegerDigits[Range[0, 15], 2, 4];
 Prepend[a, {"Pattern", "Count"}] // Grid]

yields: enter image description here

If data can contain 0 the above would need modification, e.g.:

Module[{a, f}, 
 a = {Row[#, "  "], 
     Lookup[KeyMap[f, 
       GroupBy[MapAt[Boole@*NumericQ, d, {All, All}], # &, Length]], 
      f[#], 0]} & /@ IntegerDigits[Range[0, 15], 2, 4];
 Prepend[a, {"Pattern", "Count"}] // Grid]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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