2
$\begingroup$

Im attempting to clean up a list which has missing elements by inserting the mean feature variable into the missing data

list = {{"age", "height", "weight"}, {32, 176, 75}, {27, "", 82}, {24,180, ""}, {"", 183, 89}};
list // TableForm
copylist = list
copylist[[All ;; 5, 1 ;; 3]] // TableForm;
goodsamples = Table[Cases[copylist[[All, i]], _?NumericQ], {i, 3}] // N;
goodSamplesMean = Table[Mean[goodsamples[[i]]], {i, 3}] // N
cleanlist = copylist[[All ;; 5, 1 ;; 3]] /. "" Table[goodSamplesMean[[i]], {i, 3}];

Upon running the code i recieve the error message

ReplaceAll::reps: {27.6667 ,179.667 ,82. } is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

What im aiming for is to replace the mean feature variables from

goodSamplesMean

Such that

cleanlist

appears as follows

cleanlist={{"age", "height", "weight"}, {32, 176, 75}, {27, 179.667, 82}, {24,180, 82.}, {27.6667, 183, 89}};
$\endgroup$
2
$\begingroup$

Query with the default setting for the option MissingBehavior gives the desired result:

ClearAll[replaceMissing]
replaceMissing[f_: Mean] := Prepend[Transpose[# /. _Missing -> Query[f]@# & /@ 
      Transpose[Rest[# /. "" -> Missing[]]]], #[[1]]] &;

Row[Grid[#, Dividers -> All] & /@ 
   {list, replaceMissing[]@list, replaceMissing[Total]@list}, 
   Spacer[10]]

enter image description here

Alternatively,

ClearAll[replaceMissing2]
replaceMissing2[f_: Mean] := Prepend[Transpose[# /. _Missing -> f[DeleteMissing@#] & /@ 
      Transpose[Rest[# /. "" -> Missing[]]]], #[[1]]] &;

ClearAll[replaceMissing3]
replaceMissing3[f_: Mean] := Prepend[Transpose[# /. "" -> f@DeleteCases[#, ""] & /@ 
      Transpose[Rest@#]], #[[1]]] &;

replaceMissing[]@list == replaceMissing2[]@list ==  replaceMissing3[]@list

True

$\endgroup$
0
$\begingroup$

Here are a couple of options. With

list = {{"age", "height", "weight"}, {32, 176, 75}, {27, "", 82}, {24, 180, ""}, {"", 183, 89}};
goodsamples = Select[FreeQ[#, ""] &] /@ Transpose@Rest@list;
goodSamplesMean = Mean /@ goodsamples // N;

we can do

copylist = With[{pos = Position[list, ""]},
             ReplacePart[list, Thread[pos -> goodSamplesMean[[pos[[All, 2]]]]]]
            ]
(* {{"age", "height", "weight"}, {32, 176, 75}, {27, 179.667, 82}, {24, 180, 82.}, {27.6667, 183, 89}} *)

or

copylist = list;
Function[copylist[[Sequence @@ #]] = goodSamplesMean[[#[[2]]]]] /@ Position[list, ""];
copylist
(* {{"age", "height", "weight"}, {32, 176, 75}, {27, 179.667, 82}, {24, 180, 82.}, {27.6667, 183, 89}} *)
$\endgroup$
0
$\begingroup$

Transpose is useful here.

(* From the OP: *)
list = {{"age", "height", "weight"}, {32, 176, 75}, {27, "", 82}, {24,180, ""}, {"", 183, 89}};
goodsamples = Table[Cases[list[[All, i]], _?NumericQ], {i, 3}] // N;
goodSamplesMean = Table[Mean[goodsamples[[i]]], {i, 3}] // N


Transpose[
 Table[Transpose[list][[i]] /. "" -> goodSamplesMean[[i]], {i, 3}]]

 (* {{age,height,weight},{32,176,75},{27,179.667,82},{24,180,82.},{27.6667,183,89}} *)
$\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.