ハウツー 23 ステップを1つのセッション内で順に実行します。各ステップはエンジンで実行済みで、以下の出力は実際に生成されたものです。
gdps = Map[CountryData[#, "GDP"]&, CountryData[All]];出力なし - このステップは次のステップの準備をします。
Take[gdps, 10]Take[CountryData[CountryData[All, GDP]], 10]Max[gdps]CountryData[CountryData[All, GDP]]Count[gdps, _Missing]0Length[gdps]1filteredgdps = DeleteCases[gdps, _Missing];出力なし - このステップは次のステップの準備をします。
{Length[filteredgdps], Max[filteredgdps]}{1, CountryData[CountryData[All, GDP]]}data = {{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {4, 88.6, 1.9}, {1, 62.3, 1.8}, {1, 82.7, "NA"}, {1, 35.5, 1.84}};出力なし - このステップは次のステップの準備をします。
Grid[data, Dividers -> All]Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {4, 88.6, 1.9}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}, Dividers -> All]baddata[entry_] := Not[MatchQ[entry, {1 | 2, _ ? NumberQ, _ ? NumberQ}]]出力なし - このステップは次のステップの準備をします。
DeleteCases[data, _ ? baddata]{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 35.5, 1.84}}badgroup[entry_] := Not[MatchQ[entry[[1]], 1 | 2]]出力なし - このステップは次のステップの準備をします。
filtereddata = DeleteCases[data, _ ? badgroup]{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}group1 = Select[filtereddata, #[[1]] === 1&]{{1, 71.6, 0.41}, {1, 46., 2.72}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}group1mean = Mean[Select[group1[[All, -1]], NumberQ]]2.104filtereddata[[All, 3]] = filtereddata[[All, 3]] /. "NA" -> group1mean{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}Grid[filtereddata, Dividers -> All]Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 2.104}, {1, 35.5, 1.84}}, Dividers -> All]replaceByGroup[data_, col_, groupcol_, f_] := Block[{columnvals, funvals, grouped, groups, rules},
columnvals = data[[All, {groupcol, col}]];
If[VectorQ[columnvals[[All, 2]], NumericQ],
columnvals[[All, 2]],
grouped = GatherBy[columnvals, First];
groups = grouped[[All, 1, 1]];
funvals = Table[f[Select[grouped[[i]][[All, 2]], NumericQ]], {i, Length[groups]}];
rules = Table[Rule[{groups[[i]], _ ? (Not[NumericQ[#]]&)}, {groups[[i]], funvals[[i]]}], {i, Length[groups]}];
Replace[columnvals, rules, {1}][[All, 2]]]]出力なし - このステップは次のステップの準備をします。
newdata = DeleteCases[data, _ ? badgroup]{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, NA}, {1, 35.5, 1.84}}replaceByGroup[newdata, 3, 1, Mean]{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 2.104, 1.84}replaceByGroup[newdata, 3, 1, Median]{0.41, 4.96, 0.18, 2.72, 1.06, 3.75, 1.8, 1.84, 1.84}Grid[Transpose[Table[If[j == 1, newdata[[All, j]], replaceByGroup[newdata, j, 1, Mean]], {j, Length[newdata[[1]]]}]]]Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 2.104}, {1, 35.5, 1.84}}]Grid[Transpose[{newdata[[All, 1]], replaceByGroup[newdata, 2, 1, Mean], replaceByGroup[newdata, 3, 1, Median]}]]Grid[{{1, 71.6, 0.41}, {2, 27.2, 4.96}, {2, 59.3, 0.18}, {1, 46., 2.72}, {2, 42.2, 1.06}, {1, 89.1, 3.75}, {1, 62.3, 1.8}, {1, 82.7, 1.84}, {1, 35.5, 1.84}}]